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

后端 未结 14 955
北恋
北恋 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)
                  }
        }
    
    0 讨论(0)
  • 2020-12-01 02:42
        let MainViewController  = mainStoryboard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
    
                        let nvc:rootNavigation  = mainStoryboard.instantiateViewController(withIdentifier: "rootNavigation") as! rootNavigation
                        nvc.viewControllers = [Mainmap]
                       
                        leftViewController.mainViewController = nvc
    
    UIApplication.shared.windows.first?.backgroundColor = UIColor(red: 236.0, green: 238.0, blue: 241.0, alpha: 1.0)
                        UIApplication.shared.windows.first?.rootViewController = nvc
                        UIApplication.shared.windows.first?.makeKeyAndVisible()
    
    0 讨论(0)
提交回复
热议问题