0

我必须使用 . 在第一个视图控制器的 UIWindow 上添加第二个视图控制器的视图addChildViewController。但是当我尝试这样做时,我遇到了问题。我的问题是,是否可以在 UIWindow 上添加另一个视图控制器的视图?

    secondVC = (self.storyboard?.instantiateViewController(withIdentifier: "secondVC"))!
    self.addChildViewController(secondVC)
    self.didMove(toParentViewController: self)
    draw.view.frame = CGRect(x:0, y:0, 320, 568)

    let window = UIApplication.shared.keyWindow
    window?.addSubview(secondVC.view)
4

1 回答 1

1

好吧,也许您应该实例化根 VC,然后将第二个 VC 添加到应用程序的第一个 VC 中。例如你可以写这样的东西:

//init the storyboard
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
//init the firstVC
let navigation = storyboard.instantiateInitialViewController() as! UINavigationController
let firstVC = navigation.viewControllers[0]
//init the second VC
let secondVC = (self.storyboard?.instantiateViewController(withIdentifier: "secondVC"))!
//add child VC
firstVC.addChildViewController(secondVC)
secondVC.view.frame = firstVC.view.bounds
self.view.addSubview(controller.view)
secondVC.didMove(toParentViewController: self)

这是因为主窗口关联了根 VC。keywindow 具有来自主 Storyboard 的初始视图控制器。

于 2017-09-27T13:31:37.297 回答