我正在尝试用另一个 ViewController 更改 RootViewController。但我想不通。我面临一些问题
通过上面的代码更改了 rootViewController 之后,新的 viewController 就消失了。 在控制台日志中:不鼓励在分离的 View Controller 上呈现 View Controller 。 请帮助我!
我的代码是:
func changeRootView(){
guard let delegate = UIApplication.shared.delegate else {
return
}
guard let window = (delegate as! AppDelegate).window else {
return
}
UIView.transition(with: window, duration: 0.3, options: .transitionCrossDissolve, animations: {
let lgv = DriverMainViewController()
window.rootViewController = UINavigationViewController(rootViewController: lgv)
}, completion: { completed in
SideMenuManager.menuLeftNavigationController!.dismiss(animated: true, completion: nil)
print ("changed")
})
Then changeRootView function changed App keyWindow's rootViewController
但是这个蓝色背景的 viewController 在 1 秒后就消失了。 此屏幕截图是在消失后的新 Root View Controller 。
最佳答案
我认为这里发生的事情是,当您设置窗口的 rootViewController
时,旧的 rootViewController
不再被引用,它被 ARC 删除。您可能会 try catch 外出 View Controller ,以便它在动画持续期间保持不变。试试这个:
func changeRootView(){
guard let delegate = UIApplication.shared.delegate else { return }
guard let window = (delegate as! AppDelegate).window else { return }
// capture a reference to the old root controller so it doesn't
// go away until the animation completes
let oldRootController = window.rootViewController
UIView.transition(with: window,
duration: 0.3,
options: .transitionCrossDissolve,
animations: {
let lgv = DriverMainViewController()
window.rootViewController = UINavigationViewController(rootViewController: lgv)
},
completion: { completed in
// OK, we're done with the old root controller now
oldRootController = nil
SideMenuManager.menuLeftNavigationController!.dismiss(animated: true, completion: nil)
print ("changed")
}
)
}
这段代码所做的是添加对窗口现有 Root View Controller 的引用,然后在完成 block 中捕获它以控制它存在的时间。
关于ios - 使用侧面菜单更改 Root View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948621/