UIViewController.View.Window is null in ViewDidLoad method

前端 未结 3 927
自闭症患者
自闭症患者 2021-01-03 23:15

Regardless on which controller type (UIViewController, UITableViewController), the following line always yields null in the View

相关标签:
3条回答
  • 2021-01-03 23:46

    Instead of self.view.window use

    [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] window]
    
    0 讨论(0)
  • 2021-01-04 00:03

    According to the documentation of UIView, the window property is nil if the view has not yet been added to a window which is the case when viewDidLoad is called.

    0 讨论(0)
  • 2021-01-04 00:11

    self.view.window will be available in viewDidAppear:

    override func viewDidAppear(_ animated: Bool) {
        print(self.view.window)
        let vc = self.storyboard?.instantiateViewController(identifier: "SecondViewController") as? SecondViewController
        self.view.window?.rootViewController = vc
    }
    
    0 讨论(0)
提交回复
热议问题