如何访问主窗口尝试这个,但它对我不起作用:
if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
window.addSubview(offerView)
}
我也尝试用这种方式,但我没有运气:
if let window = UIApplication.shared.windows.first {
window.addSubview(offerView)
}
注意:keyWindow
对我有用,但使用 keyWindow
可以返回键盘或 UIAlertView 窗口,位于我的应用程序窗口上方。
这是我发现的现有问题,但该答案对我不起作用
How do I get the keyWindow reference in a swift app?
在我的应用程序中,页面底部会出现一个报价 View 。该优惠将显示在屏幕的一半
最佳答案
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let rootViewController = appDelegate?.window?.rootViewController
如果 rootViewController
是自定义类,您也可以转换
let rootViewController = appDelegate?.window?.rootViewController as? YourCustomViewController
关于iOS访问主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44689850/