Development often needs to customize the popover requirements, the general implementation is nothing more than to take the APPDelegate window, add our own popover view. In fact, you can also create a UIWindow by referring to UIAlert and UIActionSheet of the system, and then adjust the window level to control the hierarchy to display the window.

That’s when a giant crater was discovered.

To create a window, initialize it:

let window = UIWindow(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
Copy the code

However, there is a problem with reading the window that is currently displayed

let topWindow = UIApplication.shared.windows.last
Copy the code

UIApplication. Shared. The number of Windows array was not to show the number of current, but it has already been created the number of Windows, such as:

let w1 = UIWindow(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
let w2 = UIWindow(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
let w3 = UIWindow(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kScreenHeight))
Copy the code

This creates multiple Windows in a row. Then print out the UIApplication. Shared. As a result of Windows:

▿ 5 elements - 0: <PaintingWindow: 0x11be07d80; baseClass = UIWindow; frame = (0 0; 375, 812); gestureRecognizers = <NSArray: 0x280bb0360>; layer = <UIWindowLayer: 0x28057d6a0>> - 1 : <UIWindow: 0x11d806d40; frame = (0 0; 375, 812); hidden = YES; gestureRecognizers = <NSArray: 0x280bb74e0>; layer = <UIWindowLayer: 0x280555f20>> - 2 : <UIWindow: 0x11dc0bc40; frame = (0 0; 375, 812); hidden = YES; gestureRecognizers = <NSArray: 0x280bf0e70>; layer = <UIWindowLayer: 0x2805551e0>> - 3 : <UIWindow: 0x11d903590; frame = (0 0; 375, 812); hidden = YES; gestureRecognizers = <NSArray: 0x280bf27c0>; layer = <UIWindowLayer: 0x28051f0a0>> - 4 : <UIWindow: 0x11d901790; frame = (0 0; 375, 812); hidden = YES; gestureRecognizers = <NSArray: 0x280bf29a0>; layer = <UIWindowLayer: 0x28051f1e0>>Copy the code

As long as the initialization of the original window, then automatically to join the UIApplication. Shared. Windows, after the release of Windows, Windows will automatically from the UIApplication. Shared. Windows is removed.