我正在升级我的应用程序以使用UIScene
iOS 13 中定义的新模式,但是该应用程序的一个关键部分已停止工作。我一直在使用 aUIWindow
来覆盖屏幕上的当前内容并向用户呈现新信息,但在我正在使用的当前测试版(iOS + XCode beta 3)中,窗口会出现,但随后会立即消失。
这是我使用的代码,现在不起作用:
let window = UIWindow(frame: UIScreen.main.bounds)
let viewController = UIViewController()
viewController.view.backgroundColor = .clear
window.rootViewController = viewController
window.windowLevel = UIWindow.Level.statusBar + 1
window.makeKeyAndVisible()
viewController.present(self, animated: true, completion: nil)
我已经尝试了很多东西,包括WindowScenes
用来展示新的UIWindow
,但找不到任何实际的文档或示例。
我的一项尝试(没有奏效 - 窗口出现并立即消失的相同行为)
let windowScene = UIApplication.shared.connectedScenes.first
if let windowScene = windowScene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let viewController = UIViewController()
viewController.view.backgroundColor = .clear
window.rootViewController = viewController
window.windowLevel = UIWindow.Level.statusBar + 1
window.makeKeyAndVisible()
viewController.present(self, animated: true, completion: nil)
}
有没有人能够在 iOS 13 beta 中做到这一点?
谢谢
编辑
从提出这个问题到发布 iOS 13 的最终版本已经过去了一段时间。下面有很多答案,但几乎所有答案都包括一件事 -添加对 UIWindow 的强/更强的引用。您可能需要包含一些与新场景相关的代码,但请先尝试添加强引用。