Force device rotation works for iPhone but not iPad

override func viewDidAppear(_ animated: Bool) {
     super.viewDidAppear(animated)
     let value = UIInterfaceOrientation.landscapeLeft.rawValue
     UIDevice.current.setValue(value, forKey: "orientation")
}


The above snippet of code works just fine when executed on any iPhone running iOS 12. But this will not work for any iPad running iOS 12. All device orientations are supported. Is there a way to force the device rotation, on a single View Controller, for both iPhone and iPad? Thank you!

Confused - you're asking about device (rotation), but your code is UI (rotation) -and- device. UI & device are not the same thing and we've always been told to just use UI...

Got it, thanks.


And when you say 'any iPad', have you enabled logging to confirm the code in question is being reached and acted on?

The

orientation
property on
UIDevice
is declared read-only. Using key-value coding (KVC) to bypass that restriction is so not supported. Frankly, I’m amazed it ever worked.

As to how to solve your real issue, I’m going to leave that to the UIKit experts to handle. I’ve moved your thread over to the App Frameworks > Cocoa Touch accordingly. And if you get completely stuck, I recommend that you open a DTS tech support incident and talk to one DTS’s UIKit specialists.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Ha, it only works until it doesn't. As in you can call it once or twice, but more times it will crash. My use-case is a one-time transition from a Portrait onboarding flow to a Landscape app. Setting (more like "telling") the device its new orientation makes the main app home view rotate to the correct presentation, even though the user is still staring at the physical device in Portrait mode. But.. try to go back again and you will quickly find yourself (if debugging) in the middle of unfamiliar (to me, I stopped after the 6502) opcodes.

Force device rotation works for iPhone but not iPad
 
 
Q