ios uiwindow弹窗,UIWindow不在iOS 13中显示内容

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)

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)

}

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

Thanks

EDIT

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.

解决方案

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()

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.

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.

Hope that helps, @SirOz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值