UIWindow不显示iOS 13中的内容 [英] UIWindow not showing over content in iOS 13

查看:911
本文介绍了UIWindow不显示iOS 13中的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在升级我的应用程序,以使用iOS 13中定义的新的UIScene模式,但是该应用程序的关键部分已停止工作. 我一直使用UIWindow覆盖屏幕上的当前内容并向用户显示新信息,但是在我正在使用的当前Beta(iOS + XCode beta 3)中,该窗口将会出现,但随后会立即消失

I am upgrading my app to use the new UIScene patterns as defined in iOS 13, however a critical part of the app has stopped working. I have been using a UIWindow to cover the current content on the screen and present new information to the user, but in the current beta I am working with (iOS + XCode beta 3) the window will appear, but then disappear straight away.

这是我使用的代码,现在不起作用:

Here is the code I was using, that now does not work:

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,但是在那里找不到任何实际的文档或示例.

I have tried many things, including using WindowScenes to present the new UIWindow, but cannot find any actual documentation or examples out there.

我的尝试之一(没有用-窗口出现并立即消失的相同行为)

One of my attempts (Did not work - same behaviour with window appearing and dismissing straight away)

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中,有人能做到这一点吗?

Has anyone been able to do this yet in iOS 13 beta?

谢谢

编辑

在提出此要求与发布iOS 13的最终版本之间已经过去了一段时间.下面有很多答案,但是几乎所有答案都包含一件事-为UIWindow添加更强/更强的引用.您可能需要包括一些与新场景有关的代码,但请尝试先添加强引用.

Some time has passed between asking this and the final version of iOS 13 being released. There are a lot of answers below, but almost all of them include one thing - Adding a strong/stronger reference to the UIWindow. You may need to include some code relating the the new Scenes, but try adding the strong reference first.

推荐答案

在升级适用于iOS 13场景模式的代码时,我遇到了同样的问题.在第二个代码段的一部分中,我设法修复了所有内容,因此我的窗口再次出现.除了最后一行,我和你做的一样.尝试删除viewController.present(...).这是我的代码:

I was experiencing the same problems while upgrading my code for iOS 13 scenes pattern. With parts of your second code snippet I managed to fix everything so my windows are appearing again. I was doing the same as you except for the last line. Try removing viewController.present(...). Here's my code:

let windowScene = UIApplication.shared
                .connectedScenes
                .filter { $0.activationState == .foregroundActive }
                .first
if let windowScene = windowScene as? UIWindowScene {
    popupWindow = UIWindow(windowScene: windowScene)
}

然后我像您一样展示它:

Then I present it like you do:

popupWindow?.frame = UIScreen.main.bounds
popupWindow?.backgroundColor = .clear
popupWindow?.windowLevel = UIWindow.Level.statusBar + 1
popupWindow?.rootViewController = self as? UIViewController
popupWindow?.makeKeyAndVisible()

无论如何,我个人认为问题出在viewController.present(...)中,因为您显示带有该控制器的窗口并立即显示一些自我",所以这取决于自我"的真正含义.

Anyway, I personally think that the problem is in viewController.present(...), because you show a window with that controller and immediately present some 'self', so it depends on what 'self' really is.

还值得一提的是,我存储了对要从控制器内部移动的窗口的引用.如果这仍然对您没有用,我只能显示使用此代码的小 repo .看看AnyPopupController.swiftPopup.swift文件.

Also worth mentioning that I store a reference to the window you're moving from inside my controller. If this is still useless for you I can only show my small repo that uses this code. Have a look inside AnyPopupController.swift and Popup.swift files.

希望有帮助,@ SirOz

Hope that helps, @SirOz

这篇关于UIWindow不显示iOS 13中的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆