如何获得多窗口SceneDelegate Xcode 11的当前等效键窗口? [英] How get current keywindow equivalent for multi window SceneDelegate Xcode 11?
问题描述
我正在将iPadOS的iOS13应用程序转换为SceneDelegate(多窗口).
I'm converting my iOS13 app for iPadOS to SceneDelegate (multi window).
如何从当前的SceneDelegate获取当前的UIWindow?
How can I get the current UIWindow from the current SceneDelegate?
我知道a可以使用UIView.window
或UIViewController.view.window
访问当前场景,但是我有一个非UI类(AppDelegate),我需要在其中获取窗口(直到iOS12之前为keyWindow)才能在顶部显示小吃店一切.
I know that a can access the current scene using UIView.window
or UIViewController.view.window
, but I have a non UI class (AppDelegate) where I need to get the window (keyWindow until iOS12) to show a snack bar on top of everything.
我曾经做过[UIApplication sharedApplication].keyWindow
,但是现在,那当然是错误的.
I used to do [UIApplication sharedApplication].keyWindow
but now, of course, that's wrong.
推荐答案
Swift 5.x/iOS 13.x
受许多解决方案的启发,找回旧的窗口属性(在connectedScenes中搜索),我实现了一些扩展:
Swift 5.x / iOS 13.x
Inspired by many solutions, to get back my old window property (searching it inside the connectedScenes) I've realized a little extension:
extension UIApplication {
var currentWindow: UIWindow? {
connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
}
}
用法:
if let window = UIApplication.shared.currentWindow {
// do whatever you want with window
}
这篇关于如何获得多窗口SceneDelegate Xcode 11的当前等效键窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!