View on top of everything: UIWindow subview VS UIViewController subview

40,233

Solution 1

The best solution I've found is to create a transparent container view, add that container to the window, and place your alert inside the container. You may then register for UIApplicationWillChangeStatusBarOrientationNotification to receive rotation events and transform the container; this allows you to independently manipulate the frame of the alert:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
     ...
     self.container = [[UIView alloc] initWithFrame:self.window.bounds];
     [self.container addSubview:self.alertView];
     [self.window addSubview:self.container];
     [[NSNotificationCenter defaultCenter] addObserver:self 
                                              selector:@selector(statusBarWillRotate:) 
                                                  name:UIApplicationWillChangeStatusBarOrientationNotification 
                                                object:nil];
     ...
}
...
- (void)statusBarWillRotate:(NSNotification *)theNotification
{
    CGFloat rotation = 0;
    switch ([notification[UIApplicationStatusBarOrientationUserInfoKey] intValue])
    {
        case UIInterfaceOrientationLandscapeLeft:
            rotation = -M_PI_2;
            break;
        case UIInterfaceOrientationLandscapeRight:
            rotation = M_PI_2;
            break;
        case UIInterfaceOrientationPortraitUpsideDown:
            rotation = M_PI;
            break;
        case UIInterfaceOrientationPortrait: 
        default:
            rotation = 0;
            break;
    }
    CGAffineTransform rotationTransform = CGAffineTransformMakeRotation(rotation);
    CGSize rotatedSize = CGRectApplyAffineTransform(self.window.bounds, rotationTransform).size;
    [UIView animationWithDuration:[UIApplication sharedApplication].statusBarOrientationAnimationDuration
                       animations:^{
         self.container.transform = rotationTransform;
         // Transform invalidates the frame, so use bounds/center
         self.container.bounds = CGRectMake(0, 0, rotatedSize.width, rotatedSize.height);
         self.container.center = CGPointMake(self.window.bounds.size.width / 2, self.window.bounds.size.height / 2);
     }];
}

You can, if you really want, create an entirely new window with its windowLevel property set to UIWindowLevelAlert, which will guarantee that the window remain above all other views, including the keyboard, but window management gets a littly tricky. The above solution should suffice for most needs.

The problem with simply adding a view controller's view to a window is that it is not guaranteed to receive all the rotation and view[Will|Did][Disa|A]ppear: messages unless it is added to the view controller hierarchy via addChildViewController: on the root view controller.

Solution 2

Only the first subview of UIWindow gets told about orientation changes. (< iOS8)

Your question is very similar to this one:

Multiple views in a UIWindow

As the answer there says, you can register for notifications of orientation changes and handle relayout of your 'on top' subview that way.

Solution 3

Here is a possible solution, I am explaining in a few steps.

  1. Start With a UIViewController inherited class instead of UIView, Lets say class named TopViewController inherited by UIViewController
  2. Since you you can not register device/interface rotation in UIView inherited class, the TopViewController class which is inherited from UIViewController has methods responding to View Rotation Events. Rotation events can be captured now.
  3. At last you can put the TopViewController view on the top of UIWindow by using the same method that you mentioned in your question .

    [[[[UIApplication sharedApplication] delegate] window] addSubview:(TopViewController object).view];
    

Hope this helps.

Share:
40,233

Related videos on Youtube

Zian Chen
Author by

Zian Chen

Updated on November 11, 2020

Comments

  • Zian Chen
    Zian Chen over 3 years

    In order to make a UIView on top of all views like the behavior of UIAlertView, the best way I see is by putting it in the app window, which is by adding the view to:

    [[[UIApplication sharedApplication] delegate] window]
    

    However, the downside I found is that it doesn't automatically take in rotation. In order for it to take in rotation, the best way to do is by adding the view on current AppDelegate window.navigationController/rootViewController, however, by doing this it will no longer be on top of everything. Let say when view is displaying and there is a modalViewController popup, the modal view will certainly cover up the view.

    My question is, is it possible to add subview to the top most window and support orientation? Do I need two set of Nib file in this case? How UIAlertView does the magic to combine the topmost & orientation support? When I look into the UIAlertView.h, I see there is actually 2 UIWindows, once is called originalWindow and another called dimWindow. Is there a place where I can find the source code for UIAlertView.m?

  • Zian Chen
    Zian Chen over 12 years
    How to register the view to the device to receive rotation event? It's not View Controller, so there is no delegate functions for it. Do I have to catch the UIDeviceOrientationDidChangeNotification to change to corresponding orientation?
  • khunshan
    khunshan almost 10 years
    UIViewController's view property wont rotate also. Unfortunately. developer.apple.com/library/ios/qa/qa1688/_index.html
  • asendra
    asendra over 9 years
    I have a problem with this solution. I'm not able to register touches in that top most container view. UIButtons doesn't work, for example.
  • Austin
    Austin over 9 years
    @asendra Sounds like your window isn't the key window, but you may want to post your own question with more detail.