• Stars
    star
    5,481
  • Rank 7,498 (Top 0.2 %)
  • Language
    Java
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A splash screen for react-native, hide when application loaded ,it works on iOS and Android.

react-native-splash-screen

Download PRs Welcome react-native-splash-screen release 语言 中文 License MIT 原理 解析 Flutter

A splash screen API for react-native which can programatically hide and show the splash screen. Works on iOS and Android.

Content

Changes

For React Native >= 0.47.0 use v3.+, for React Native < 0.47.0 use v2.1.0

Examples

react-native-splash-screen-Android react-native-splash-screen-iOS

Installation

First step(Download):

Run npm i react-native-splash-screen --save

Second step(Plugin Installation):

Automatic installation

react-native link react-native-splash-screen or rnpm link react-native-splash-screen

Manual installation

Android:

  1. In your android/settings.gradle file, make the following additions:
include ':react-native-splash-screen'   
project(':react-native-splash-screen').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-splash-screen/android')
  1. In your android/app/build.gradle file, add the :react-native-splash-screen project as a compile-time dependency:
...
dependencies {
    ...
    implementation project(':react-native-splash-screen')
}
  1. Update the MainApplication.java file to use react-native-splash-screen via the following changes:
// react-native-splash-screen >= 0.3.1
import org.devio.rn.splashscreen.SplashScreenReactPackage;
// react-native-splash-screen < 0.3.1
import com.cboy.rn.splashscreen.SplashScreenReactPackage;

public class MainApplication extends Application implements ReactApplication {

    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        @Override
        public boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
            new SplashScreenReactPackage()  //here
            );
        }
    };

    @Override
    public ReactNativeHost getReactNativeHost() {
        return mReactNativeHost;
    }
}

iOS:

  1. cd ios
  2. run pod install

OR

  1. In XCode, in the project navigator, right click LibrariesAdd Files to [your project's name]

  2. Go to node_modulesreact-native-splash-screen and add SplashScreen.xcodeproj

  3. In XCode, in the project navigator, select your project. Add libSplashScreen.a to your project's Build PhasesLink Binary With Libraries

  4. To fix 'RNSplashScreen.h' file not found, you have to select your project → Build Settings → Search Paths → Header Search Paths to add:

    $(SRCROOT)/../node_modules/react-native-splash-screen/ios

Third step(Plugin Configuration):

Android:

Update the MainActivity.java to use react-native-splash-screen via the following changes:

import android.os.Bundle; // here
import com.facebook.react.ReactActivity;
// react-native-splash-screen >= 0.3.1
import org.devio.rn.splashscreen.SplashScreen; // here
// react-native-splash-screen < 0.3.1
import com.cboy.rn.splashscreen.SplashScreen; // here

public class MainActivity extends ReactActivity {
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.show(this);  // here
        super.onCreate(savedInstanceState);
    }
    // ...other code
}

iOS:

Update AppDelegate.m with the following additions:

#import "AppDelegate.h"

#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import "RNSplashScreen.h"  // here

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // ...other code

    [RNSplashScreen show];  // here
    // or
    //[RNSplashScreen showSplash:@"LaunchScreen" inRootView:rootView];
    return YES;
}

@end

Getting started

Import react-native-splash-screen in your JS file.

import SplashScreen from 'react-native-splash-screen'

Android:

Create a file called launch_screen.xml in app/src/main/res/layout (create the layout-folder if it doesn't exist). The contents of the file should be the following:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/launch_screen" android:scaleType="centerCrop" />
</RelativeLayout>

Customize your launch screen by creating a launch_screen.png-file and placing it in an appropriate drawable-folder. Android automatically scales drawable, so you do not necessarily need to provide images for all phone densities. You can create splash screens in the following folders:

  • drawable-ldpi
  • drawable-mdpi
  • drawable-hdpi
  • drawable-xhdpi
  • drawable-xxhdpi
  • drawable-xxxhdpi

Add a color called primary_dark in app/src/main/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="primary_dark">#000000</color>
</resources>

Optional steps:

If you want the splash screen to be transparent, follow these steps.

Open android/app/src/main/res/values/styles.xml and add <item name="android:windowIsTranslucent">true</item> to the file. It should look like this:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <!--设置透明背景-->
        <item name="android:windowIsTranslucent">true</item>
    </style>
</resources>

To learn more see examples

If you want to customize the color of the status bar when the splash screen is displayed:

Create android/app/src/main/res/values/colors.xml and add

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="status_bar_color"><!-- Colour of your status bar here --></color>
</resources>

Create a style definition for this in android/app/src/main/res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="SplashScreenTheme" parent="SplashScreen_SplashTheme">
        <item name="colorPrimaryDark">@color/status_bar_color</item>
    </style>
</resources>

Change your show method to include your custom style:

SplashScreen.show(this, R.style.SplashScreenTheme);

iOS

Customize your splash screen via LaunchScreen.storyboard or LaunchScreen.xib

Learn more to see examples

Usage

Use like so:

import SplashScreen from 'react-native-splash-screen'

export default class WelcomePage extends Component {

    componentDidMount() {
    	// do stuff while splash screen is shown
        // After having done stuff (such as async tasks) hide the splash screen
        SplashScreen.hide();
    }
}

API

Method Type Optional Description
show() function false Open splash screen (Native Method )
show(final Activity activity, final boolean fullScreen) function false Open splash screen (Native Method )
hide() function false Close splash screen

Testing

Jest

For Jest to work you will need to mock this component. Here is an example:

// __mocks__/react-native-splash-screen.js
export default {
  show: jest.fn().mockImplementation( () => { console.log('show splash screen'); } ),
  hide: jest.fn().mockImplementation( () => { console.log('hide splash screen'); } ),
}

Troubleshooting

Splash screen always appears stretched/distorted

Add the ImageView with a scaleType in the launch_screen.xml, e.g.:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
>
  <ImageView 
    android:src="@drawable/launch_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
  >
  </ImageView>
</FrameLayout>

Contribution

Issues are welcome. Please add a screenshot of you bug and a code snippet. Quickest way to solve issue is to reproduce it in one of the examples.

Pull requests are welcome. If you want to change the API or do something big it is best to create an issue and discuss it first.


MIT Licensed

More Repositories

1

TakePhoto

一款用于在Android设备上获取照片(拍照或从相册、文件中选择)、裁剪图片、压缩图片的开源工具库
Java
7,233
star
2

RNStudyNotes

React Native 研究与实践
Objective-C
4,019
star
3

awesome-flutter-cn

一个很棒的Flutter学习资源,官方教程,插件,工具,文章,App,视频教程等的资源列表
2,883
star
4

GitHubPopular

这是一个用来查看GitHub最受欢迎与最热项目的App,它基于React Native支持Android和iOS双平台。#适配RN最新版在这里☞#
JavaScript
2,880
star
5

react-native-awesome

React Native 学习资源精选仓库(汇聚知识,分享精华)汇集了各类react-native学习资料、工具、组件、开源App、资源下载、以及相关新闻等,只求精不求全。
1,737
star
6

react-native-easy-toast

A react native module to show toast like android, it works on iOS and Android.
JavaScript
1,093
star
7

react-native-check-box

Checkbox component for react native, it works on iOS and Android.
JavaScript
513
star
8

flutter_splash_screen

A splash screen for flutter, hide when application loaded ,it works on iOS and Android.
Java
116
star
9

GroupListView

仿支付宝账单列表效果
Java
64
star
10

react-native-event-bus

Event bus for react native, cross-interface communication solution, it works on iOS and Android.
JavaScript
46
star
11

GitHubTrending

This is a library for fetch object data from "https://github.com/trending".
JavaScript
38
star
12

flutter_color_plugin

A color parse package for flutter,it works on iOS and Android.
Dart
22
star
13

IncrementalUpdate

Android省流量更新、增量更新、补丁生成与应用
C
21
star
14

HeadSetControl

耳机线控实例,蓝牙耳机按钮监听
Java
21
star
15

hijson

HiJson is a lightweight JSON parsing library that can be used for HarmonyOS, Android, and Java
Java
16
star
16

Ios-HomeUI

基于AutoLayout的Ios首页导航框架,支持滑动切换,TabBar切换,侧拉栏菜单等
Objective-C
14
star
17

BreakPointUploadUtil

Java断点上传工具
Java
13
star
18

crazycodeboy.github.io

https://crazycodeboy.github.io/
HTML
11
star
19

WaitScreen

Android页面加载遮罩
Java
9
star
20

ListViewPlus

带有上拉加载下拉刷新的ListView
Java
9
star
21

openai_flutter

A openai library for flutter,it works on iOS,Android,Web,macOS and Windows.
Dart
7
star
22

underline_indicator

A underline indicator
Dart
7
star
23

awesome-harmonyos

一个很棒的HarmonyOS学习资源,官方教程,插件,工具,文章,App,视频教程等的资源列表
6
star
24

PUtils

Android,Http,orm...
Java
4
star
25

KeyboardObserver

Ios键盘输入框自适应
Objective-C
4
star
26

flutter_overlay

A transparent floating layer for flutter which can programmatically show and close the child. Works on iOS,Android and Web.
Dart
3
star
27

react-native-safe-area-plus

A flexible way to handle safe area, also works on Android and iOS
JavaScript
2
star
28

flutter_hi_cache

A cache manager based on shared_preferences. Works on iOS,Android and Web.
Dart
2
star
29

gitbook-plugin-single-page

JavaScript
1
star
30

react-native-take-photo

React Native版TakePhoto@https://github.com/crazycodeboy/TakePhoto
1
star
31

react-native-navbar-plus

A react native module to show navbar,it works on iOS and Android.
JavaScript
1
star
32

flutter_nested

Lists that support nested scrolling can customize the header and the number of lines displayed per line.
Dart
1
star
33

react-native-isiphonex-device

A react native module to replace DeviceInfo.isIPhoneX_deprecated.
TypeScript
1
star
34

DigitalTimer

自定义数字时钟
Java
1
star