Как установить rootViewController в Scene Delegate iOS 13 - PullRequest
0 голосов
/ 01 октября 2019

До изменений в UIKit iOS 13, как я могу установить rootViewController в 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 }

    }

Ответы [ 2 ]

3 голосов
/ 01 октября 2019

К вашему сведению, поскольку SwiftUI не использует раскадровки, если вы просто создадите новый проект SwiftUI, он даст вам код;все, что вам нужно сделать, это заменить UIHostingViewController на желаемый корневой VC следующим образом:

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()
    }
  }
0 голосов
/ 01 октября 2019

Вот так:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

   var window: UIWindow?
   func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
         // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
         // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
         // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }

        let rootVC = self.window?.rootViewController 
    }
    // ... the rest of SceneDelegate
}

Как видно здесь .

...