UIView subview of UIWindow doesn't rotate
Asked Answered
C

2

7

I'm adding a UIView to a UIWindow that's not the keyWindow. I'd like the UIView to rotate when the device rotates. Any special properties on the window or the view I need to set? Currently the view is not rotating.

I'm aware of the fact that only the first subview of the application's keyWindow is told about device rotations. As a test I added my view to the first subview of the keyWindow. This causes the view to rotate. However the view being a subview of the keyWindow's first subview won't work for various aesthetic reasons.

An alternative approach is observing device orientation changes in the view and writing the rotation code myself. However I'd like to avoid writing this code if possible (having an additional window is cleaner in my opinion).

Castor answered 5/1, 2012 at 4:14 Comment(0)
V
7

UIView doesn't handle rotations, UIViewController does. So, all you need is to create a UIViewController, which implements shouldAutorotateToInterfaceOrientation and sets this controller as a rootViewController to your UIWindow

Something like that:

    UIViewController * vc = [[[MyViewController alloc] init] autorelease];

    vc.view.frame = [[UIScreen mainScreen] bounds];
    vc.view.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];

    //you vc.view initialization here

    UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    window.windowLevel = UIWindowLevelStatusBar;
    window.backgroundColor = [UIColor clearColor];
    [window setRootViewController:vc];
    [window makeKeyAndVisible];

and I used this MyViewController, cause I want it to reflect changes of main application

    @interface MyViewController : UIViewController

    @end

    @implementation MyViewController

    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
    {
        UIWindow *window = ((UIWindow *)[[UIApplication sharedApplication].windows objectAtIndex:0]);
        UIViewController *controller = window.rootViewController;

        if (!controller) {
            NSLog(@"%@", @"I would like to get rootViewController of main window");
            return YES;
        }
        return [controller shouldAutorotateToInterfaceOrientation:toInterfaceOrientation];
    }
    @end

but you can always just return YES for any orientation or write your logic, if you wish.

Voussoir answered 17/6, 2012 at 0:6 Comment(3)
I'm having a similar problem -- UIWindow and its views do not seem to be rotating -- and this solution does not seem to work for me. I make the window's rootViewController be a controller that is successfully rotating its subviews, but the window's subviews do not rotate at all.Whitney
And they should not, cause they have not attached to UIViewController and only UIViewController manages the rotationVoussoir
This is a great answer, thanks. It makes rotation on a secondary Window "Just Work". To remove the window from screen later, just set its rootViewController to nil, and the reference to itself too, so ARC can deallocate it.Formation
L
2

You can add below code to your project

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

and created the function to handle it.

Likely answered 5/1, 2012 at 4:30 Comment(1)
I'd prefer getting the rotation for "free" rather than writing didRotateCastor

© 2022 - 2024 — McMap. All rights reserved.