如何将新添加的 UIWindow 保持在顶部

How do I keep newly added UIWindow on top

我的应用程序中有一些要求,其中我需要添加新的 window 以获得一些额外的功能,我能够成功地做到这一点。启动应用程序后,我根据要求添加和关闭 windows。但我想知道如何让当前的 window 始终位于其他 windows 之上以避免冲突?在 iOS 中有什么方法可以使呈现的内容 window 始终位于其他内容之上?

看看makeKeyAndVisibleAPI

func makeKeyAndVisible()

Shows the window and makes it the key window.
This is a convenience method to show the current window and position it in front of all other windows at the same level or lower. If you only want to show the window, change its isHidden property to false.

另外,UIApplication.windowsproperty,是可见和隐藏的数组windows

var windows: [UIWindow]

您可以迭代此数组并找到一个 window,您将其设置为键 window。

最后看看 .windowLevel UIWindow property,它管理 window 在 z 轴上的位置。

var windowLevel: UIWindow.Level { get set }

Window levels provide a relative grouping of windows along the z-axis. All windows assigned to the same window level appear in front of (or behind) all windows assigned to a different window level. The ordering of windows within a given window level is not guaranteed.

The default value of this property is normal. For a list of other possible window levels, see UIWindow.Level.

windows 的顺序由 UIWindow.windowLevel 控制。

myWindow.windowLevel = .statusBar + 1