iosswiftipadorientationscreen-rotation

Lock orientation and disable rotation on iPad


I've got the following code which works fine on iPhone, but not on iPad. It should lock the orientation on landscape and disable autorotation only for THIS viewcontroller (not for the whole app - NOT changing attributes in the project/info.plist file):

override var shouldAutorotate : Bool {
        return false
}

and in viewDidLoad():

let landscapeValue = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(landscapeValue, forKey: "orientation")

How can I lock the orientation to landscape and disable rotation for my current ViewController (like it is already perfectly working on iPhone)?

My Specs:

XCode - 9.1 Build 9B55

Swift - 4.0.2

Tested on iPad Air - iOS 10.3.3


Solution

  • The answer to my question was actually quite simple. You have to activate Requires full screen in Deployement Info of your project. After that the iPad behaves the same as the iPhone with this code.