react-native-splash-screen

repository·master·Indexed 26 days ago

https://github.com/crazycodeboy/react-native-splash-screen

A React Native library (v3.3.0) that provides a programmatic API to show and hide splash screens on iOS and Android. It allows for a smoother transition from the native launch screen to the application, featuring a JavaScript API with show() and hide() methods, and detailed configuration guides for Android layout XMLs and iOS LaunchScreen storyboards.

Tokens
3.4K
Snippets
12
Records
21
Agent score
90%

What's inside react-native-splash-screen

  1. Configure Android Splash Screen

    master

    To enable the splash screen on Android, update MainActivity.java:

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

    Customizing the Layout: Create a file named launch_screen.xml in your Android resources to define the splash screen layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns: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>

    Preventing White Flash (Translucent Theme): To avoid a brief white screen during app startup, add android:windowIsTranslucent to your theme in android/app/src/main/res/values/styles.xml:

    <resources>
        <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="android:windowIsTranslucent">true</item>
        </style>
    </resources>
    SplashScreen.show(this);
  2. Configure react-native-splash-screen for Android

    master

    Follow these steps to manually link and configure the plugin for Android:

    In android/settings.gradle:

    include ':react-native-splash-screen'   
    project(':react-native-splash-screen').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-splash-screen/android')

    In android/app/build.gradle:

    dependencies {
        implementation project(':react-native-splash-screen')
    }

    2. Register the package

    Update MainApplication.java to include SplashScreenReactPackage in your getPackages() list:

    // react-native-splash-screen >= 0.3.1
    import org.devio.rn.splashscreen.SplashScreenReactPackage;
    
    // Inside getPackages()
    return Arrays.<ReactPackage>asList(
        new MainReactPackage(),
        new SplashScreenReactPackage()
    );

    3. Initialize the Splash Screen

    Update MainActivity.java to show the splash screen when the activity is created:

    import android.os.Bundle;
    // react-native-splash-screen >= 0.3.1
    import org.devio.rn.splashscreen.SplashScreen;
    
    public class MainActivity extends ReactActivity {
       @Override
        protected void onCreate(Bundle savedInstanceState) {
            SplashScreen.show(this);
            super.onCreate(savedInstanceState);
        }
    }
  3. Configure iOS Splash Screen

    master

    To enable the splash screen on iOS, update AppDelegate.m:

    #import "AppDelegate.h"
    #import <React/RCTBundleURLProvider.h>
    #import <React/RCTRootView.h>
    #import "RNSplashScreen.h"  // Add this line
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // ...other code
    
        [RNSplashScreen show];  // Add this line at the very end
        return YES;
    }
    
    @end

    Customizing the Splash Screen: Use LaunchScreen.storyboard or LaunchScreen.xib in Xcode to customize the visual appearance.

    [RNSplashScreen show];
  4. Manually install react-native-splash-screen on iOS

    master

    Follow these steps for manual iOS installation:

    1. In Xcode's Project Navigator, click LibrariesAdd Files to [your project's name].
    2. Add SplashScreen.xcodeproj located at node_modules/react-native-splash-screen/SplashScreen.xcodeproj.
    3. In Xcode, go to Build PhasesLink Binary With Libraries and add libSplashScreen.a.
    4. Troubleshooting 'SplashScreen.h' file not found: If you encounter this error, add the following to your Header Search Paths in Build Settings: $(SRCROOT)/../node_modules/react-native-splash-screen/ios
  5. Configure react-native-splash-screen for iOS

    master

    1. Install Pods

    cd ios
    run pod install

    Alternatively, via Xcode:

    1. Right-click LibrariesAdd Files to [your project's name] and add node_modules/react-native-splash-screen/ios/SplashScreen.xcodeproj.
    2. Add libSplashScreen.a to your project's Build PhasesLink Binary With Libraries.
    3. If you encounter 'RNSplashScreen.h' file not found, add $(SRCROOT)/../node_modules/react-native-splash-screen/ios to your project's Build SettingsSearch PathsHeader Search Paths.

    2. Initialize the Splash Screen

    Update AppDelegate.m:

    #import "RNSplashScreen.h"
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        // ... other code
        [RNSplashScreen show];
        return YES;
    }
    
    @end
  6. Add a LaunchScreen.storyboard to iOS

    master

    Since React Native projects do not include a LaunchScreen.storyboard by default, you must create one manually in Xcode to support splash screens on iOS.

    1. Open your project's iOS folder in Xcode.
    2. In the left-hand file navigation panel, right-click your project folder.
    3. Select New File... and create a new Launch Screen file (which will be saved as LaunchScreen.storyboard).
  7. Apply LaunchScreen.storyboard to your iOS Target

    master

    To ensure iOS uses your new storyboard as the app launch screen, you must register it in your project settings:

    1. Select your project in the Xcode project navigator.
    2. Select your app under TARGETS.
    3. Go to the General tab.
    4. Locate the Launch Screen File setting and select LaunchScreen (or your specific storyboard filename) from the dropdown.
    5. Important: Delete the app from your simulator/device and re-run the project to see the changes.
  8. Customize Android Splash Screen Assets and Layout

    master

    1. Create Layout File

    Create app/src/main/res/layout/launch_screen.xml:

    <?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>

    2. Add Images

    Place your splash image (e.g., launch_screen.png) in the appropriate drawable folders (e.g., drawable-hdpi, drawable-xhdpi, etc.).

    3. Optional: Transparent Background

    In android/app/src/main/res/values/styles.xml, add:

    <item name="android:windowIsTranslucent">true</item>

    4. Optional: Status Bar Color

    Add a color to app/src/main/res/values/colors.xml:

    <resources>
        <color name="status_bar_color">#YOUR_COLOR_HERE</color>
    </resources>

    And define a style in styles.xml:

    <style name="SplashScreenTheme" parent="SplashScreen_SplashTheme">
        <item name="colorPrimaryDark">@color/status_bar_color</item>
    </style>

    Then call SplashScreen.show(this, R.style.SplashScreenTheme); in MainActivity.java.