UIDevice.current.setValue (value, forKey: "orientation") does not work if the phone is held in the landscape and tilted 45 - 90 degrees

I use

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

to make the display controller view appear in landscape mode. This is part of the navigation controller thread. This works as expected, but if the phone is held in the landscape and called between 45 and 90 degrees, the view will be presented in the portrait. The method is called and the expected value is correct, but the landscape does not change. This can be reproduced in a bare-bone project with a navigation controller.

Does anyone know what might cause this behavior?

+5
source share
2 answers

You can override

 func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) 

as described in https://developer.apple.com/reference/uikit/uitraitenvironment/1623516-traitcollectiondidchange

how 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 orientation as desired. In addition, in Xcode, click on your target, select the General tab, and select only one permitted orientation.

+1
source

Try the following:

 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 to Setting device orientation in Swift iOS

+1
source

Source: https://habr.com/ru/post/1268268/


All Articles