I'm using
let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
to force the presenting view controller to show in landscape right. This is part of a navigation controller flow. This works as expected, but if the phone is held in landscape and titled between 45 and 90 degrees the view will present in portrait. The method is called and the expected value is correct, but no change to landscape occurs. This can be reproduced in a bare bones project with a navigation controller.
Does anyone have any idea what might cause this behavior?
You could override
func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
as described in https://developer.apple.com/reference/uikit/uitraitenvironment/1623516-traitcollectiondidchange
as in
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
[super traitCollectionDidChange: previousTraitCollection];
if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
|| (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {
// your custom implementation here
}
}
to maintain your orientation as desired. Alternatively, in XCode, click on your target, choose the General tab, and choose only one allowed orientation.
Try this:
open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
get {
return UIInterfaceOrientationMask.landscapeRight
}
}
open override var shouldAutorotate: Bool {
get {
return false
}
}
open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
get {
return UIInterfaceOrientation.landscapeRight
}
}
From @Dragos answer on Setting device orientation in Swift iOS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With