Open leecade opened 8 years ago
ref: https://github.com/mehcode/rn-splash-screen
import SplashScreen from 'rn-splash-screen' // Hide the active splash screen SplashScreen.hide()
Add a slash image in [project_name]/android/app/src/main/res/drawable/slash.png
[project_name]/android/app/src/main/res/drawable/slash.png
example:
Add the image as a windowBackground in [project_name]/android/app/src/main/res/values/styles.xml
[project_name]/android/app/src/main/res/values/styles.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowBackground">@drawable/splash</item> </style>
Override the onCreate to show the splash screen in [project_name]/android/app/src/main/java/com/[project_name]/MainActivity.java
onCreate
[project_name]/android/app/src/main/java/com/[project_name]/MainActivity.java
// [...] import com.mehcode.reactnative.splashscreen.SplashScreen; public class MainActivity extends ReactActivity { // [...] @Override protected void onCreate(Bundle savedInstanceState) { // Show the js-controlled splash screen SplashScreen.show(this); // After react is initialized; set our background color (override splash screen theme) getReactNativeHost().getReactInstanceManager().addReactInstanceEventListener(new ReactInstanceManager.ReactInstanceEventListener() { @Override public void onReactContextInitialized(ReactContext context) { // Hide the native splash screen getWindow().getDecorView().setBackgroundColor(Color.WHITE); } }); super.onCreate(savedInstanceState); // [...] } // [...] }
LaunchScreen.xib
Create LaunchImage
LaunchImage
Prepare images
ios/[project name]/Images.xcassets └── LaunchImage.launchimage ├── Contents.json ├── Default-568h@2x.png ├── Default-667h@2x.png ├── Default-Landscape-736h@3x.png ├── Default-Portrait-736h@3x.png ├── Default-Portrait.png └── Default@2x.png
Contents.json
{ "images": [ { "extent": "full-screen", "idiom": "iphone", "filename": "Default-568h@2x.png", "minimum-system-version": "7.0", "orientation": "portrait", "scale": "2x", "subtype": "retina4" }, { "extent": "full-screen", "idiom": "iphone", "filename": "Default-667h@2x.png", "minimum-system-version": "8.0", "orientation": "portrait", "scale": "2x", "subtype": "667h" }, { "extent": "full-screen", "idiom": "iphone", "filename": "Default-Landscape-736h@3x.png", "minimum-system-version": "8.0", "orientation": "landscape", "scale": "3x", "subtype": "736h" }, { "extent": "full-screen", "idiom": "iphone", "filename": "Default-Portrait-736h@3x.png", "minimum-system-version": "8.0", "orientation": "portrait", "scale": "3x", "subtype": "736h" }, { "extent": "full-screen", "idiom": "iphone", "filename": "Default@2x.png", "minimum-system-version": "7.0", "orientation": "portrait", "scale": "2x" } ], "info": { "version": 1, "author": "xcode" } }
In project settings, tap Use Asset Catalog for Launch Images under App Icons and Launch Images and have it use the newly created LaunchImage.
Show the splash screen after rootView created in [project_name]/ios/[project_name]/AppDelegate.m
rootView
[project_name]/ios/[project_name]/AppDelegate.m
// [...] #import "RCTSplashScreen.h" - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // [...] RCTRootView *rootView = // [...] // Show splash screen (rn-splash-screen) [RCTSplashScreen show:rootView]; // [...] }
ref: https://github.com/mehcode/rn-splash-screen
Usage
Android step
Add a slash image in
[project_name]/android/app/src/main/res/drawable/slash.png
Add the image as a windowBackground in
[project_name]/android/app/src/main/res/values/styles.xml
Override the
onCreate
to show the splash screen in[project_name]/android/app/src/main/java/com/[project_name]/MainActivity.java
iOS step
LaunchScreen.xib
Create
LaunchImage
Prepare images
Contents.json
In project settings, tap Use Asset Catalog for Launch Images under App Icons and Launch Images and have it use the newly created LaunchImage.
Show the splash screen after
rootView
created in[project_name]/ios/[project_name]/AppDelegate.m