How set rootViewController in Scene Delegate iOS 13

后端 未结 4 2054
无人共我
无人共我 2020-12-10 20:40

Before changes in UIKit iOS 13 how I can set rootViewController at SceneDelegate?

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window:          


        
相关标签:
4条回答
  • 2020-12-10 21:08

    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()
        }
      }
    
    0 讨论(0)
  • 2020-12-10 21:13

    If you don't need to support multi window support just ignore it. I follow up this answer.

    When you start new project with it's automatically creates manifest and SceneDelegate class. After ignore multi window support, you can use app delegate window again like before.

    0 讨论(0)
  • 2020-12-10 21:24

    If you ever find yourself needing to access the window from the delegate (both iOS12 & iO13)to change the rootviewcontroller here's a couple of extensions I use:

       extension UIViewController {
            var appDelegate: AppDelegate {
            return UIApplication.shared.delegate as! AppDelegate
        }
        
        var sceneDelegate: SceneDelegate? {
            guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                let delegate = windowScene.delegate as? SceneDelegate else { return nil }
             return delegate
        }
    }
    
    extension UIViewController {
        var window: UIWindow? {
            if #available(iOS 13, *) {
                guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
                    let delegate = windowScene.delegate as? SceneDelegate, let window = delegate.window else { return nil }
                       return window
            }
            
            guard let delegate = UIApplication.shared.delegate as? AppDelegate, let window = delegate.window else { return nil }
            return window
        }
    }
    
    0 讨论(0)
  • 2020-12-10 21:32

    Like this:

    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
    }
    

    As seen here.

    0 讨论(0)
提交回复
热议问题