如何在Scene Delegate iOS 13中设置rootViewController [英] How set rootViewController in Scene Delegate iOS 13

查看:111
本文介绍了如何在Scene Delegate 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不会使用情节提要,它将为您提供代码;您需要做的就是用所需的根VC替换UIHostingViewController,如下所示:

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

这篇关于如何在Scene Delegate iOS 13中设置rootViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆