Search code examples
iosuiviewcontrolleruiinterfaceorientation

Using the same UIInterfaceOrientation for all UIViewControllers


Right now any new UIViewController added to my storyboard needs to have a class that has the following code:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

Which is fine to maintain via copy/paste on each class, but is that really necessary? Is there not a faster way?


Solution

  • You can set the supported interface orientations across the whole app in the info.plist file using the Supported interface orientations key (you can also set this graphically in the project summary panel).

    If that doesn't help (which it apparently doesn't from your comment below) you could add your code as a category on UIViewController, thereby saving you from having to copy and paste it into every controller.

    If that seems a bit radical, you could instead create a UIViewController subclass containing that method and use it as the superclass for all of your other controllers (BaseViewController would be a good name for it).