Search code examples
iosswiftdevice-orientation

How to add device orientation to project programmatically in Swift?


Is there a way to add or remove these components programatically?
enter image description here


Solution

  • No, not exactly. You need to set up all the possible user interface orientations your app's view controllers MIGHT support in info.plist. Once you've done that there are view controller methods the system calls to see what orientations an individual view controller supports. (See supportedInterfaceOrientations in the docs.)

    As of iOS 8 we're not supposed to use rotation methods any more, so check the docs carefully. It looks to me like supportedInterfaceOrientations is still supported, but the willRotate..., willAnimateRotation..., didRotateFromInterfaceOrientation, etc, are deprecated.

    I suggest reading about user interface rotation in the UIViewController class reference (in Xcode or in Apple's online documentation on the web.)