问题
Scenario: Create and display an overlay (ancillary) UIWindow.
Related Discussion: How do I toggle between UIWindows?
Code:
fileprivate func displayOverLay() {
let myWindow = {() -> UIWindow in
let cWindow = UIWindow(frame: CGRect(x: 10, y: 200, width: 300, height: 300))
cWindow.backgroundColor = UIColor.gray.withAlphaComponent(0.5)
cWindow.tag = 100
return cWindow
}()
myWindow.makeKeyAndVisible()
myWindow.windowLevel = UIWindowLevelAlert
let storyboard = UIStoryboard(name: "Hamburger", bundle: nil)
let vc = storyboard.instantiateInitialViewController()
myWindow.rootViewController = vc
return
}
Here's the shared UIApplication window status:
(lldb) po UIApplication.shared.windows
▿ 2 elements
- 0 : <UIWindow: 0x7fcffc804860; frame = (0 0; 375 667); autoresize = W+H; gestureRecognizers = <NSArray: 0x600000051280>; layer = <UIWindowLayer: 0x60000003e840>>
- 1 : <UIWindow: 0x7fcff9407240; frame = (10 200; 300 300); tag = 100; gestureRecognizers = <NSArray: 0x608000052810>; layer = <UIWindowLayer: 0x608000220420>>
(lldb) po UIApplication.shared.keyWindow!
<UIWindow: 0x7fcff9407240; frame = (10 200; 300 300); tag = 100; gestureRecognizers = <NSArray: 0x608000052810>; layer = <UIWindowLayer: 0x608000220420>>
Here's the result: no gray window sceen
Important Note: If I brake at the end of the displayOverLay() and do a 'po myWindow', and then continue, I do get the display.
So it appears that I'm missing some sort of event to have the UIWindow appear.
What is needed to force the new UIWindow w/contents to actually display?
回答1:
You need to keep a reference to the created window, otherwise ARC will take care of disposing the window after you return from your function.
You could for instance return the newly created window from your function and store it in a property of the class from which your are calling the function.
来源:https://stackoverflow.com/questions/44220382/why-do-i-not-see-my-new-uiwindow