LoginSignup
5
7

More than 5 years have passed since last update.

ReactNativeでスプラッシュスクリーンをいい感じに出す

Last updated at Posted at 2018-11-22

RNスプラッシュスクリーンの作り方

React、iOS、Android開発未経験の覚書です。
間違えている可能性もあります。
その時は編集リクエストかコメントで教えてください。

モジュール公式を日本語で噛み砕いたような感じです。
詳しくはモジュールのgithubへ
https://github.com/crazycodeboy/react-native-splash-screen

拡張モジュールを導入

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

各OSのネイティブコードをちょっと修正する

Android

MainActivity.java

import com.facebook.react.ReactActivity;

import android.os.Bundle; // 追記
import org.devio.rn.splashscreen.SplashScreen; // 追記

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

iOS

AppDelegate.m
#import "AppDelegate.h"

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

@implementation AppDelegate

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

    [RNSplashScreen show];  // 追記
    return YES;
}

@end

起動後のscreenとなる所でスプラッシュスクリーンを非表示にする

App.js

export default class App extends Component<Props> {

// コンポーネントをマウントできたらスプラッシュを消す
  componentDidMount() {
    SplashScreen.hide();
  }

// other

StatusBarとメイン背景の色を同色にする

App.js

// other
  render() {
    return (
      <View style={styles.container}>
        <StatusBar barStyle="light-content" backgroundColor="#212121" /> // 追記
        <Text style={styles.welcome}>Welcome to React Native!</Text>
        <Text style={styles.instructions}>To get started, edit App.js</Text>
        <Text style={styles.instructions}>{instructions}</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#212121' // ステータスバーと色を合わせる
  },
  // other

各OS毎にスプラッシュスクリーンを設定する

Android

app/src/main/res/layout/launch_screen.xmlを配置する

launch_screen.xml
<?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>

app/src/main/res/配下にdrawableフォルダを配置する

  • drawable-xhdpi
  • drawable-xxhdpi

中にlaunch_screen.pngでスプラッシュ画像を置いておく

iOS

XcodeでlaunchScreen.xibを編集する

5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7