如果在iOS13之前,你是這樣使用彈框
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()
那么在iOS13時(shí),你會(huì)發(fā)現(xiàn)UI界面并沒有任何反應(yīng),之所以無效,是因?yàn)槟闶褂昧薸OS 13 的SceneDelegate,
此時(shí)你需要使用下面這種方式來創(chuàng)建window
let windowScene = UIApplication.shared
.connectedScenes
.filter { $0.activationState == .foregroundActive }
.first
if let windowScene = windowScene as? UIWindowScene {
// 該window是全局變量
window = UIWindow(windowScene: windowScene)
window?.frame = UIScreen.main.bounds
window?.backgroundColor = .clear
}
之后展示的時(shí)候
window?.windowLevel = UIWindow.Level.statusBar + 1
window?.rootViewController = viewController
window?.makeKeyAndVisible()
此時(shí)你會(huì)發(fā)現(xiàn)makeKeyAndVisible又起作用了。。。
另:
在iOS13中,keyWindow已被廢棄,若使用了SceneDelegate, 則在iOS13上獲取會(huì)得到nil,可以使用
UIApplication.shared.windows.first
或
var window: UIWindow?
if #available(iOS 13.0, *) {
window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window
} else {
window = UIApplication.shared.keyWindow
}