93

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • Using multiple UIWindows in iOS applications http://shaune.com.au/using-multiple-uiwindows-in-ios-applications/ – onmyway133 Aug 28 '14 at 09:35

3 Answers3

112

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.
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • I wouldn't have had the courage to use another UIWindow if not for your answer. [This](http://stackoverflow.com/a/6698102/458193) was also extremely helpful because the second UIWindow wouldn't autorotate on its own. – Dan Abramov Sep 20 '12 at 04:55
  • 1
    Very enlightening post. But what happens if you resign this window and make the underlying window key and visible and this underlying window's rootViewController has a modal controller currently presented? Will it present the modal view controller back? What happens in our application is that rootViewController is presented not the modal one... – Nava Carmon Dec 19 '12 at 19:00
  • @Krumelur This is fine. I have a doubt. It is, say the 2nd UIWindow is displayed on top of all the current views. When the user clicks one button on the view which is in 1st UIWindow which is presenting one more viewcontroller. This 2nd UIWindow would hide. How to make this UIWindow to be displayed always throughout the whole app. – Easwaramoorthy Kanagaraj Jun 29 '13 at 11:21
  • 1
    I use UIWindow all the time for implementing custom styled alert views and iphone popovers. It helps always making the view fullscreen, containing a fullscreen view and embedding another "content"-view inside so the outer view will catch any touch events (and so i can add a nice shadow and dim my other content ^^) – Martin Ullrich Jul 15 '13 at 09:17
  • 3
    It should be noted that a second window is not retained just for being visible (in contrast with regular views being retained by their superview). – Rivera Mar 03 '14 at 01:45
  • In my experience I didn't want the new window to become the keyWindow because its purpose was just to show an overlay that the user should not be able to touch. Having this non-key overlay window caused some problems back in the time of iOS5~iOS6 when apple introduced the dictionary model view controller that appears when tapping on "define" when a word is selected in a text view. – nacho4d Feb 26 '15 at 01:13
  • I don't know if the problem still exists now but in iOS5 iOS6 dismissing the dictionary view controller made my application irresponsive. It was still alive but not receiving events. Non of my windows received events. So I ended up by creating my overlay view in the first window :( – nacho4d Feb 26 '15 at 01:16
  • I wish at that time documentation was better and tell what is the effect of having a non-key window on top of key window. -- I don't know the answer even now BTW – nacho4d Feb 26 '15 at 01:17
  • third bullet point seems incorrect. I created two windows with rootViewController having textfields but one of the window was initWithFrame where frame was inset of the main screen bounds. Toggled the key windows and found that both the textfeids can be edited. – BangOperator May 06 '16 at 09:38
27

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • Err, alright. Thanks. I knew that. Can you get a bit into detail wrt my questions? Do you know how to get RID of a seconds UIWindow that was added by makeKeyAndVisible? – Krumelur Nov 22 '11 at 20:21
  • Set the second UIWindow's hidden property to YES. – rob mayoff Nov 22 '11 at 21:08
  • Thanks. And I show the window by making it key and visible? And I hide it by making ANOTHER window visible and just disposing the other one? How can I animate a UIWindow like UIActionSheet does it? – Krumelur Nov 23 '11 at 09:13
  • Be careful with this. If at != UIWindowLevelNormal, the keyboard will show underneath it on < iOS7. – Shawn Mar 27 '14 at 14:25
0

Here is Apple's Documentation for better understanding UIWindow: https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/WindowAndScreenGuide/WindowScreenRolesinApp/WindowScreenRolesinApp.html

One good though specific reason to use multiple instances of UIWindow is when you need to video record the app screen. You may not want to include certain elements (recording button, recording status, etc.) in the final recorded video, so you can put those elements in a separate UIWindow on top.

In fact, if you are using ReplayKit, you will have to use a separate UIWindow for these excluded UI elements. More info here: https://medium.com/ar-tips-and-tricks/how-to-record-a-screen-capture-with-replaykit-whilst-hiding-the-hud-element-bedcca8e31e

Ivy Xing
  • 284
  • 2
  • 8