当前景中有多个连接的场景处于活动状态时,获取当前聚焦的UIScene [英] Getting currently focused UIScene when multiple connected scenes are active in foreground

查看:338
本文介绍了当前景中有多个连接的场景处于活动状态时,获取当前聚焦的UIScene的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在连接多个场景时获取当前的UIScene时出现问题.

I have a problem getting the current UIScene when multiple scenes are connected.

具体地说,我的应用程序(iOS 13)在iPad上支持多个窗口.当我并排有两个窗口并且访问应用程序连接的场景(使用UIApplication.shared.connectedScenes)时,所有场景的activationState都设置为.foregroundActive.但是我实际上只与其中之一互动.

Specifically, my app (iOS 13) supports multiple windows on the iPad. When I have two windows side by side and I access the apps connected scenes (with UIApplication.shared.connectedScenes), all of them have the activationState set to .foregroundActive. But I am actually only interacting with one of them.

我在stackoverflow上找到了一些代码来检索当前的关键窗口(不记得是谁发布了它),但是如您所见,它将始终返回两个窗口之一.

I found some code here on stackoverflow to retrieve the current key window (can't remember who it was that posted it) but as you can see it will always return one of the two windows.

let keyWindow: UIWindow? = UIApplication.shared.connectedScenes
    .filter({$0.activationState == .foregroundActive})
    .map({$0 as? UIWindowScene})
    .compactMap({$0})
    .first?.windows
    .filter({$0.isKeyWindow}).first

我是否缺少确定与之交互的最后一个窗口的方法,或者多个.foregroundActive状态是否存在错误?

Am I missing some method to determine the last window that was interacted with or are the multiple .foregroundActive states a bug?

谢谢!

推荐答案

我通过重构代码找到了解决问题的方法,以便现在我可以通过任意视图的window属性访问当前的UIScene s窗口.视图控制器.这样,我就不再需要keyWindow了.

I found the solution to my problem by refactoring my code so that now I access the current UIScenes window through the window attribute of any view or view controller. This way I don't need the keyWindow anymore.

我认为彼此相邻打开的漫游器窗口具有activationState .foregroundActive

I think it's probably correct that bot windows open next to each other have the activationState .foregroundActive

万一有人需要,这是两个扩展,它们帮助我从应用程序中的任何地方转到当前窗口:

In case anyone needs it, these are the two extensions that helped me get to to current window from anywhere in the app:

extension UIViewController {
  var window: UIWindow? {
    return view.window
  }
}

extension UIScene {
  var window: UIWindow? {
    return (delegate as? SceneDelegate)?.window
  }
}

但是,如果您没有访问UIViewUIViewControllerUIScene的权限,问题仍然存在.例如,在没有引用视图或场景的数据模型中.然后,我看不到始终访问用户与之交互的正确窗口的方法.

But the problem still stands if you don't have access to a UIView, UIViewController or UIScene. For example inside a data model with no reference to a view or scene. Then I don't see a way the consistently accessing the correct window the user is interacting with.

这篇关于当前景中有多个连接的场景处于活动状态时,获取当前聚焦的UIScene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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