react-native 配置启动图支持ios和android

2018-10-11  本文已影响0人  叫兽儿

配置启动图我使用的是第三方模块react-native-splash-screen,更加详细的请到 github地址进一步查看,我们这里提供基本的配置以及个别问题的解决方案

当前环境

安装第三方模块 react-native-splash-screen

 yarn add react-native-splash-screen
  
 [or]
  
 npm install react-native-splash-screen -S

手动配置

android配置

 ...

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

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

  ...
  ...

  import org.devio.rn.splashscreen.SplashScreenReactPackage;

  public class MainApplication extends Application implements ReactApplication {

      private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
         
         ...
         
         @Override
         protected List<ReactPackage> getPackages() {
             return Arrays.<ReactPackage>asList(
                new SplashScreenReactPackage()  //here
             );
         }
         
         ...
      };
  }

  ...

启动图相关配置

配置ios

  ...

  #import  "SplashScreen.h"   // here

  ...

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

      [SplashScreen show];  // here

      ...

      return YES;
  }

  @end

配置android

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

  <color name="primary_dark">#000000</color>

  ...

在项目中关闭启动图

配置完毕之后重启你的app发现启动图已经展现出来了但是迟迟没有关闭
我们需要在项目中进行控制,将他在合适的时候进行关闭,方法如下:

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

  export default class App extends Component {

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

End~

上一篇 下一篇

猜你喜欢

热点阅读