React Native中如何在Xcode里移除或隐藏LaunchScreen.storyboard
在React Native环境中彻底清理Xcode里的LaunchScreen.storyboard配置
你已经移除了LaunchScreen.storyboard文件,接下来需要清理相关配置并设置自定义启动屏,步骤如下:
1. 移除Info.plist中的启动屏配置
- 打开项目的
Info.plist文件:- 若用Xcode可视化编辑,找到Launch screen interface file base name这一项,直接删除;
- 若用源码编辑,删除以下代码段:
<key>UILaunchStoryboardName</key> <string>LaunchScreen</string>
2. 配置自定义启动屏
如果要使用自定义图片或纯代码启动屏,可在AppDelegate中添加临时启动视图逻辑,直到React Native加载完成:
Objective-C 示例
在AppDelegate.m中:
- 先在类声明中添加属性:
@interface AppDelegate () @property (nonatomic, strong) UIWindow *launchWindow; @end - 在
didFinishLaunchingWithOptions方法中添加:// 创建临时启动窗口 self.launchWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIImageView *launchImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CustomLaunchImage"]]; launchImageView.contentMode = UIViewContentModeScaleAspectFill; [self.launchWindow addSubview:launchImageView]; [self.launchWindow makeKeyAndVisible]; // React加载完成后隐藏(或设置延迟隐藏) dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [UIView animateWithDuration:0.5 animations:^{ self.launchWindow.alpha = 0.0; } completion:^(BOOL finished) { self.launchWindow = nil; }]; });
Swift 示例
在AppDelegate.swift中:
- 添加属性:
private var launchWindow: UIWindow? - 在
application(_:didFinishLaunchingWithOptions:)方法中添加:// 创建临时启动窗口 launchWindow = UIWindow(frame: UIScreen.main.bounds) let launchImageView = UIImageView(image: UIImage(named: "CustomLaunchImage")) launchImageView.contentMode = .scaleAspectFill launchWindow?.addSubview(launchImageView) launchWindow?.makeKeyAndVisible() // 延迟隐藏启动视图 DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) { UIView.animate(withDuration: 0.5) { self.launchWindow?.alpha = 0.0 } completion: { _ in self.launchWindow = nil } }
3. 清理项目缓存
- 执行
pod install(项目使用CocoaPods时); - 在Xcode中按
Cmd + Shift + K清理Build Folder; - 重新编译项目,确保配置生效。
内容的提问来源于stack exchange,提问作者Asad Kabir
相关产品推荐
相关产品推荐

