本文介绍了如何在场景委托 iOS 13 中设置 rootViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 UIKit iOS 13 更改之前,如何在 SceneDelegate 设置 rootViewController?
Before changes in UIKit iOS 13 how I can set rootViewController at SceneDelegate?
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
@available(iOS 13.0, *)
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
}
推荐答案
仅供参考,因为 SwiftUI 不使用故事板,如果你只是创建一个新的 SwiftUI 项目,它会给你代码;您需要做的就是将 UIHostingViewController 替换为您想要的根 VC,如下所示:
FYI since SwiftUI doesn't use storyboards if you just make a new SwiftUI project it will give you the code; all you need to do is replace the UIHostingViewController with your desired root VC like this:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = MyRootViewController()
self.window = window
window.makeKeyAndVisible()
}
}
这篇关于如何在场景委托 iOS 13 中设置 rootViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!