Skip to content

Repository files navigation

react-native-splash-screen

DownloadPRs Welcomereact-native-splash-screen release语言 中文License MIT原理 解析

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-Androidreact-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 = newFile(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 {
...
compileproject(':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.1importorg.devio.rn.splashscreen.SplashScreenReactPackage;
// react-native-splash-screen < 0.3.1importcom.cboy.rn.splashscreen.SplashScreenReactPackage;
publicclassMainApplicationextendsApplicationimplementsReactApplication {
privatefinalReactNativeHostmReactNativeHost = newReactNativeHost(this) {
@OverrideprotectedbooleangetUseDeveloperSupport() {
returnBuildConfig.DEBUG;
}
@OverrideprotectedList<ReactPackage> getPackages() {
returnArrays.<ReactPackage>asList(
newMainReactPackage(),
newSplashScreenReactPackage() //here
);
}
};
@OverridepublicReactNativeHostgetReactNativeHost() {
returnmReactNativeHost;
}
}

iOS:

  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 'SplashScreen.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:

importandroid.os.Bundle; // hereimportcom.facebook.react.ReactActivity;
// react-native-splash-screen >= 0.3.1importorg.devio.rn.splashscreen.SplashScreen; // here// react-native-splash-screen < 0.3.1importcom.cboy.rn.splashscreen.SplashScreen; // herepublicclassMainActivityextendsReactActivity {
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
SplashScreen.show(this); // heresuper.onCreate(savedInstanceState);
}
// ...other code
}

iOS:

Update AppDelegate.m with the following additions:

#import"AppDelegate.h"
#import"RCTRootView.h"
#import"SplashScreen.h"// here@implementationAppDelegate
- (BOOL)application:(UIApplication *)applicationdidFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...other code
[SplashScreen show]; // herereturnYES;
}
@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"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/launch_screen">
</LinearLayout>

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

To modify the status bar color, you can add a color called primary_dark in color.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. -->
<stylename="AppTheme"parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. --><!--设置透明背景-->
<itemname="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>
<colorname="status_bar_color"><!-- Colour of your status bar here --></color>
</resources>

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

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

Change your show method to include your custom style:

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

iOS

Customize your splash screen via LaunchImage or LaunchScreen.xib,

Learn more to see examples

Usage

Use like so:

importSplashScreenfrom'react-native-splash-screen'exportdefaultclassWelcomePageextendsComponent{componentDidMount(){// do stuff while splash screen is shown// After having done stuff (such as async tasks) hide the splash screenSplashScreen.hide();}}

API

MethodTypeOptionalDescription
show()functionfalseOpen splash screen (Native Method )
hide()functionfalseClose splash screen

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

About

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

Resources

Stars

0 stars

Watchers

4 watching

Forks

Releases

Packages

Contributors

Languages