Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages, problems, examples of adding another UIWindow to an iOS app?

Recently I've been wondering about the fact that that an iOS app only has one UIWindow. It does not seem to be an issue to create another UIWindow and place it on screen.

My question is kind of vague, but I'm interested in:

  • What could I potentially achieve with a second UIWindow that cannot be done in other ways?
  • What can go wrong when using multiple UIWindow instances?
  • I have seen that people use a 2nd UIWindow to display popover like views on iPhone. Is this a good way of doing it? Why? Why not?
  • Are there other examples where it is making perfectly sense to have another UIWindow?

It's not that I'm missing something. I have never felt the need to create another UIWindow instance but maybe it would allow doing amazing things I'm not aware of! :-)

I'm hoping that it might help me solve this problem: I need to add a "cover view" over whatever is currently displayed. It should also work if there are already one or more modal controllers presented. If I add a UIView to the root controller's view, the modal controllers sit on top, so do the popover controllers. If I present the cover view modally and there is already a modal controller, only part of the screen is covered.

like image 454
Krumelur Avatar asked Nov 22 '11 19:11

Krumelur


People also ask

Can we have multiple UIWindow in iOS?

Yes, you can have multiple windows. A key window is the one who receives the user input.

What is UIWindow in iOS?

The backdrop for your app's user interface and the object that dispatches events to your views. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.1+ tvOS 9.0+

Is UIWindow a UIView?

It's not an either/or kind of thing. Your app must have a UIWindow instance that is the container for the UIView instances you present. An app can either load the UIWindow instance from a nib file, or it can create it programmatically.


2 Answers

Starting with Rob's answer I played around a bit and would like to write down some notes for others trying to get information on this topic:

  • It is not a problem at all to add another UIWindow. Just create one and makeKeyAndVisible. Done.
  • Remove it by making another window visible, then release the one you don't need anymore.
  • The window that is "key" receives all the keyboard input.
  • UIWindow covers everything, even modals, popovers, etc. Brilliant!
  • UIWindow is always portrait implicitly. It does no rotate. You'll have to add a controller as the new window's root controller and let that handle rotation. (Just like the main window)
  • The window's level determines how "high" it gets displayed. Set it to UIWindowLevelStatusBar to have it cover everything. Set its hidden property to NO.
  • A 2nd UIWindow can be used to bring views on the screen that float on top of everything. Without creating a dummy controller just to embed that in a UIPopoverController.
  • It can be especially useful on iPhone where there is no popover controller but where you might want to mimic something like it.
  • And yes, it solved of course my problem: if the app resigns activation, add a cover window over whatever is currently shown to prevent iOS from taking a screenshot of your app's current content.
like image 130
Krumelur Avatar answered Oct 17 '22 04:10

Krumelur


A UIWindow can float above other UI elements like the system keyboard.

To address your last paragraph: Make a UIWindow with the same frame as your main window. Set its windowLevel property to UIWindowLevelStatusBar. Set its hidden property to NO.

like image 30
rob mayoff Avatar answered Oct 17 '22 04:10

rob mayoff