mHo*_*ins 15 uiwindow ios swift ios13 uiscenedelegate
我正在升级应用程序以使用UISceneiOS 13中定义的新模式,但是该应用程序的关键部分已停止工作。我一直在使用a UIWindow来覆盖屏幕上的当前内容并向用户显示新信息,但是在我正在使用的当前Beta(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)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了许多方法,包括使用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)
}
Run Code Online (Sandbox Code Playgroud)
在iOS 13 beta中,有人能做到这一点吗?
谢谢
编辑
在提出此要求与发布iOS 13的最终版本之间已经过去了一段时间。下面有很多答案,但几乎所有答案都包含一件事- 向UIWindow添加更强/更强的引用。您可能需要包括一些与新场景有关的代码,但是请尝试先添加强引用。
gla*_*oss 12
在升级适用于iOS 13场景模式的代码时,我遇到了同样的问题。使用第二部分代码的一部分,我设法修复了所有内容,因此我的窗口再次出现。除了最后一行,我和你一样。尝试删除viewController.present(...)。这是我的代码:
let windowScene = UIApplication.shared
.connectedScenes
.filter { $0.activationState == .foregroundActive }
.first
if let windowScene = windowScene as? UIWindowScene {
popupWindow = UIWindow(windowScene: windowScene)
}
Run Code Online (Sandbox Code Playgroud)
然后我像您一样介绍它:
popupWindow?.frame = UIScreen.main.bounds
popupWindow?.backgroundColor = .clear
popupWindow?.windowLevel = UIWindow.Level.statusBar + 1
popupWindow?.rootViewController = self as? UIViewController
popupWindow?.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)
无论如何,我个人认为问题出在哪里viewController.present(...),因为您显示带有该控制器的窗口并立即显示一些“自我”,所以这取决于“自我”的真正含义。
还值得一提的是,我存储了对从控制器内部移动的窗口的引用。如果这仍然对您没有用,我只能显示使用此代码的小型仓库。看看里面AnyPopupController.swift和Popup.swift文件。
希望有帮助,@ SirOz
基于所有建议的解决方案,我可以提供自己的代码版本:
private var window: UIWindow!
extension UIAlertController {
func present(animated: Bool, completion: (() -> Void)?) {
window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIViewController()
window.windowLevel = .alert + 1
window.makeKeyAndVisible()
window.rootViewController?.present(self, animated: animated, completion: completion)
}
open override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
window = nil
}
}
Run Code Online (Sandbox Code Playgroud)
如何使用:
// Show message (from any place)
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Button", style: .cancel))
alert.present(animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
以下是在 iOS 13 上的新窗口中显示视图控制器的步骤:
UIWindowScene。extension UIWindowScene {
static var focused: UIWindowScene? {
return UIApplication.shared.connectedScenes
.first { $0.activationState == .foregroundActive && $0 is UIWindowScene } as? UIWindowScene
}
}
Run Code Online (Sandbox Code Playgroud)
UIWindow为重点场景创建。if let window = UIWindowScene.focused.map(UIWindow.init(windowScene:)) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
UIViewController在那个窗口。let myViewController = UIViewController()
if let window = UIWindowScene.focused.map(UIWindow.init(windowScene:)) {
window.rootViewController = myViewController
window.makeKeyAndVisible()
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7800 次 |
| 最近记录: |