iOS13+Xcode11切换RootViewController时出现黑屏动画问题求助
解决iOS13+切换UIWindow RootViewController时的黑屏动画问题
我之前也踩过这个坑!在iOS13及以后的版本中,Apple调整了UIWindow设置rootViewController的默认行为——系统会自动添加一个过渡动画,这就是你看到黑屏动画的根源。下面给你两种靠谱的解决方法:
方法一:用UIView.performWithoutAnimation包裹设置逻辑
这是最直接有效的方式,让所有视图操作跳过动画执行:
UIView.performWithoutAnimation { let tabbarVc: EPTabbarViewControllers = storyBoardIdentifiers.main.instantiateViewController(withIdentifier: "EPTabbarViewControllers") as! EPTabbarViewControllers let navControl = UINavigationController(rootViewController: tabbarVc) navControl.navigationBar.isHidden = false window?.rootViewController = navControl // 强制让窗口成为主窗口并显示,避免布局延迟 window?.makeKeyAndVisible() }
方法二:配合iOS13的SceneDelegate正确处理(多场景适配)
如果你的项目已经适配了iOS13的多窗口场景,在SceneDelegate中设置rootVC时也用同样的动画禁用逻辑,同时确保窗口和场景关联正确:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { guard let windowScene = (scene as? UIWindowScene) else { return } let window = UIWindow(windowScene: windowScene) self.window = window UIView.performWithoutAnimation { let tabbarVc = storyBoardIdentifiers.main.instantiateViewController(withIdentifier: "EPTabbarViewControllers") as! EPTabbarViewControllers let navControl = UINavigationController(rootViewController: tabbarVc) navControl.navigationBar.isHidden = false window.rootViewController = navControl window.makeKeyAndVisible() } }
为什么这样能解决?
iOS13开始,系统给rootViewController的切换添加了默认过渡动画,而UIView.performWithoutAnimation会临时禁用所有视图动画,让rootVC的切换瞬间完成,完全消除黑屏过渡的问题。
另外要注意:如果你的项目还需要兼容iOS12及以下版本,这个方法同样适用,不会对旧系统的行为产生影响。
内容的提问来源于stack exchange,提问作者Drashti Javiya
相关产品推荐
相关产品推荐

