在UIKit iOS 13中进行更改之前,如何在SceneDelegate中设置rootViewController?

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不使用 Storyboard ,如果您仅创建一个新的SwiftUI项目,它将为您提供代码;您需要做的就是用所需的根VC替换UIHostingViewController,如下所示:

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()
    }
  }

关于swift - 如何在Scene Delegate iOS 13中设置rootViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58188296/

10-11 12:18