UIWindow not showing(swift 3-iOS)

Hi, I'm trying to make a second UIWindow above the main window but it is not showing, this is the code in appDelegate:

var window2:UIWindow?
func applicationDidFinishLaunching(_ application: UIApplication) {
        window2?.windowLevel = UIWindowLevelAlert
        window2?.isHidden = false
        window2?.frame = UIScreen.main.bounds
        window2?.backgroundColor = UIColor.blue
}


When checking with:

print(UIApplication.shared.windows.count)

It printed 2 but there was no extra window seen.

And when I deleted the initial window in appDelegate, the whole screen became black(no window displayed).

I tried searching online but couldn't find any solution. Can anyone help?

Thanks.

Answered by just.do.it in 173600022

Hi,


I don't see any code which creates the window.

var window2:UIWindow? -- declares a window, but don't create one.


window2?.windowLevel = UIWindowLevelAlert
window2?.isHidden = false
window2?.frame = UIScreen.main.bounds
window2?.backgroundColor = UIColor.blue

since window2 is nil nothing happens here.


Dirk

In iOS, you have a single window that has a root view controller. In turn, that view controller will allow you to present things to the screen. For an alert, you'll need to use a UIAlertController.


I suggest going over some basic tutorials on iOS development.

I'm not making an alert but just trying to make a floating window with a view controller:

window2.windowLevel = CGFloat.greatestFiniteMagnitude

No matter what I set the window level to be, it does not show up on the screen.

Anyone knows what might be wrong?

Again, you cannot create additional windows in iOS. On that platform, the UIWindow represents the screen as a whole.


If you want to display some content over an existing view, you will need to use a view or layer, managed by one of the view or layer control mechanisms that iOS provide.

Is there anyway to present a "floating" viewController that remain on top not affected by segues?

Is there anyway to present a "floating" viewController that remain on top not affected by segues?

OK, here’s some UIKit advice from a Networking Guy™:

  • There are circumstances where it makes sense to create multiple UIWindows on iOS (for example, when supporting a second screen). However, they are few and far between and this is not one of them.

  • If you want to support custom nesting of view controllers beyond what’s supported by the built-in view controllers (like the tab bar controller), you need to look at implementing a custom container view controller. This is discussed in the View Controller Programming Guide for iOS.

ps I’ve moved your thread to App Frameworks > Cocoa Touch because you’re more likely to find UIKit experts there.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

Hi,


I don't see any code which creates the window.

var window2:UIWindow? -- declares a window, but don't create one.


window2?.windowLevel = UIWindowLevelAlert
window2?.isHidden = false
window2?.frame = UIScreen.main.bounds
window2?.backgroundColor = UIColor.blue

since window2 is nil nothing happens here.


Dirk

That's not correct. I've an iOS app which creates an additional window on demand. The windows is shown (althougs it's background color is clearColor), but a rootViewController assigned to it is shown.


Dirk

Thanks to all your replies, I found out that I have to declare the window outside the methods or appdelegate to make it display.

I really wish people would stop talking about things they have no idea about it. iOS supports multiple windows quite adequately, and indeed, even the most benign iOS app is multi-window: the keyboard resides in a window, so do menus, the "Define"/"Look Up" dictionary view, UIAlertView, the status bar, etc. Windows can be fullscreen or otherwise.

Sometimes, maintaining multiple windows is very benefitial, whether it be for animations/transitions, snapshots, UI hiding, or just for a completely seperate view controller stack.


In the future, please refrain from helping in areas where you have no experience in.

I am not sure why this is the official advice from Apple, when Apple engineers use multiple windows quite broadly. Why doesn't Apple just (publicly) embrace multi-window design, as already supported by the API?

Windows must be retained in order to remain visible. If you create a window, but do not retain it, it will not appear on screen.

UIWindow not showing(swift 3-iOS)
 
 
Q