NEW: Master Swift design patterns with my latest book! >>

How to lock a view controller’s orientation using supportedInterfaceOrientations

Written by Paul Hudson    @twostraws

At the project level you can configure which orientations your whole app should support, but sometimes you want individual view controllers to support a subset of those. For example, you might want most of your app to work in any orientation, but one part to work specifically in portrait.

To configure this, you need to override the supportedInterfaceOrientations property in your UIViewController subclass, returning whichever orientations you want. Probably the most common use for this is to support all orientations for iPads, but .allButUpsideDown on iPhone.

Here’s some example code doing just that:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

Available from iOS 7.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Get my latest video for free

Learn about value types, functional programming, and protocol-oriented programming in this new video – it's free!

Click here to visit the Hacking with Swift store >>