【问题标题】:How to Push ViewController in new window如何在新窗口中推送 ViewController
【发布时间】:2020-02-17 06:45:53
【问题描述】:
我需要通过代码在新窗口中推送viewController。我正在执行以下代码,但它不起作用。
let win = UIWindow(frame: UIScreen.main.bounds)
let nc = UINavigationController(rootViewController: self)
nc.view.backgroundColor = .clear
win.rootViewController = nc
win.windowLevel = UIWindow.Level.alert + 1
win.makeKeyAndVisible()
有人知道如何在新窗口中推送视图控制器吗?
【问题讨论】:
标签:
ios
uinavigationcontroller
【解决方案1】:
Swift 5.x
以下代码有效
取全局变量:
var window2 : UIWindow?
修改后的代码如下
let win = UIWindow(frame: UIScreen.main.bounds)
let nc = UINavigationController()
nc.pushViewController(self, animated: true)
nc.navigationBar.isHidden = true
nc.view.backgroundColor = .clear
win.rootViewController = nc
window2 = win
win.windowLevel = UIWindow.Level.alert + 1
win.makeKeyAndVisible()
然后关闭我在下面做的新窗口
if let nc = window2?.rootViewController as? UINavigationController {
nc.popViewController(animated: true)
}
window2?.resignKey()
window2 = nil
注意:它不显示推送和弹出默认动画,而是目前它正在工作,没有任何类型的动画。
参考: To create a new UIWindow over the main window