Search code examples
iphoneiosobjective-crotationios7

Handling autorotation for one view controller in iOS7


I've read many answers on SO but I can't seem to get autorotation working on iOS7.

I only need one view controller to rotate, so I don't want to set rotation settings in my Info.plist.

As I understand Apple's documentation, a single view controller can override global rotations settings (from Info.plist) by simply overriding two methods. Info.plist is set to only allow Portrait, and my view controller implements the following methods:

- (NSUInteger)supportedInterfaceOrientations
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

- (BOOL)shouldAutorotate
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
    return true;
}

I'm seeing those NSLog statements upon rotation but nothing rotates.

If I do configure Info.plist with the proper rotation settings, my view will rotate, but not if I try and rely on my view controller.

Not sure if it matters, but the view I'm trying to rotate is from a .xib using auto layout.

Also, my ViewController is being presented modally and is contained in a navigation controller. I've tried just presenting the view controller by itself and that doesn't work. I've also tried adding a category to UINavigationController to get it's autorotation directions from it's topViewController.


Solution

  • You need to set the plist value to all possible values, then limit them as you see fit (in the Navigation Controllers and TabBar Controllers. From the UIViewController class description:

    In iOS 6 and later, your app supports the interface orientations defined in your app’s Info.plist file. A view controller can override the supportedInterfaceOrientations method to limit the list of supported orientations. Typically, the system calls this method only on the root view controller of the window or a view controller presented to fill the entire screen; child view controllers use the portion of the window provided for them by their parent view controller and no longer participate directly in decisions about what rotations are supported. The intersection of the app’s orientation mask and the view controller’s orientation mask is used to determine which orientations a view controller can be rotated into.