react-native-splash-screen中SplashScreen.hide()在iOS设备无法生效求助
React Native SplashScreen.hide() 在iOS不生效的解决方案
1. 修复AppDelegate中的窗口初始化逻辑
你的AppDelegate代码缺少UIWindow和根视图控制器的初始化步骤,导致RNSplashScreen无法正确挂载到视图层级中。修改后的代码如下:
#import <Firebase.h> #import "AppDelegate.h" #import "RNSplashScreen.h" #import <React/RCTBundleURLProvider.h> #import <React/RCTRootView.h> @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [FIRApp configure]; self.moduleName = @"spengucomputercourse"; self.initialProps = @{}; // 初始化窗口与根视图控制器 self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL:self.sourceURLForBridge:nil moduleName:self.moduleName initialProperties:self.initialProps launchOptions:launchOptions]; UIViewController *rootViewController = [UIViewController new]; rootViewController.view = rootView; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; [RNSplashScreen show]; // 确保在窗口设置完成后调用 return YES; } - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge { #if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index"]; #else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"]; #endif } @end
2. 验证启动页Storyboard配置
打开Xcode,完成以下检查:
- 找到
LaunchScreen.storyboard,选中主视图控制器 - 在Identity Inspector面板中,将
Storyboard ID设置为LaunchScreen(与库默认配置一致) - 确保视图控制器的Class为
UIViewController,不要使用自定义类
3. 调整JS端隐藏时机(可选)
如果RN初始化较慢,过早调用hide()可能失效,可以监听AppState在应用激活后再隐藏:
import React, {useEffect} from 'react'; import Navigations from './src/Routes/Navigations'; import SplashScreen from 'react-native-splash-screen'; import {LogBox, AppState} from 'react-native'; const App = () => { LogBox.ignoreAllLogs(); useEffect(() => { const appStateListener = AppState.addEventListener('change', (state) => { if (state === 'active') { SplashScreen.hide(); appStateListener.remove(); // 仅执行一次 } }); return () => appStateListener.remove(); }, []); return <Navigations />; };
4. 重新安装iOS依赖
进入iOS目录执行pod安装,确保库的原生代码正确集成:
cd ios && pod install && cd ..
内容的提问来源于stack exchange,提问作者Atul Singour
相关产品推荐
相关产品推荐

