Search code examples
iosswiftuiwindow

How do I keep newly added UIWindow on top


There are requirements in my application wherein I need to add the new window for few additional functionalities and I am able to do that successfully. After launching the application I am adding and dismissing windows as per the requirement. But I want to know that how do I keep my current window always on top of other windows if any to avoid conflicts? Are there any ways in iOS where we can keep the presenting window always on top of other ones?


Solution

  • Take a look at makeKeyAndVisible API

    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.

    Also, UIApplication has .windows property, which is an array of visible and hidden windows

    var windows: [UIWindow]
    

    You can iterate this array and find a window which you are gonna to set as a key window.

    And lastly take a look at .windowLevel UIWindow property, it manages position of the window in the z-axis.

    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.