iOS 13: Swift - 'Set application root view controller programmatically' does not work

后端 未结 14 1098
北恋
北恋 2020-12-01 01:43

I have following code in my AppDelegate.swift to setup root view controller for an iOS application. But it does not work. It follows Target structure (defined under General

14条回答
  •  悲哀的现实
    2020-12-01 02:42

    var window: UIWindow?
    

    has been moved from AppdDelegate.swift to SceneDelegate.swift.

    So I used rootViewController in the Scene Delegate class and it works -

       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 }
    
            if let tabBarController = window?.rootViewController as? UITabBarController {
                      let storyboard = UIStoryboard(name: "Main", bundle: nil)
                      let vc = storyboard.instantiateViewController(identifier: "NavController")
    
                      vc.tabBarItem = UITabBarItem(tabBarSystemItem: .topRated, tag: 1)
                      tabBarController.viewControllers?.append(vc)
                  }
        }
    

提交回复
热议问题