react-native-splash-screen-newarch
v1.0.1
Published
A splash screen for react-native, hide when application loaded ,it works on iOS and Android.
Maintainers
Readme
react-native-splash-screen-newarch
React Native 新架构启动屏库。
兼容性
[email protected]- React Native
>= 0.84.0 - New Architecture(TurboModule / Codegen)
安装
npm i react-native-splash-screen-newarch使用
import SplashScreen from 'react-native-splash-screen-newarch';
SplashScreen.hide();Android 配置
在 MainActivity.kt 的 super.onCreate 前调用:
import android.os.Bundle
import com.facebook.react.ReactActivity
import org.devio.rn.splashscreen.SplashScreen
class MainActivity : ReactActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
SplashScreen.show(this, true)
super.onCreate(savedInstanceState)
}
}创建 android/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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/launch_screen" />
</RelativeLayout>iOS 配置
1. 配置 LaunchScreen.storyboard
使用静态布局,图片放在 Assets.xcassets。
2. 在 AppDelegate.swift 调用
import react_native_splash_screen在 React Native 启动后调用:
RNSplashScreen.show()关键片段(AppDelegate.swift,RN 0.84 模板):
import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
import react_native_splash_screen
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
) -> Bool {
...
factory.startReactNative(
withModuleName: "你的应用名",
in: window,
launchOptions: launchOptions
)
RNSplashScreen.show() // <--- 确保在 React Native 启动后调用
return true
}
}
class ReactNativeDelegate: RCTDefaultReactNativeFactoryDelegate {
...
}API
show()hide()
MIT License
