I am trying to force only one view in my application on landscape mode, I am calling
override func shouldAutorotate() -> Bool { print("shouldAutorotate") return false } override func supportedInterfaceOrientations() -> Int { print("supportedInterfaceOrientations") return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue) } override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation { return UIInterfaceOrientation.LandscapeLeft }
The view is launched in the portrait mode, and keep rotating when I change the device orientation.
The shouldAutorotate is never called.
Any help would be appreciated.
On an iPhone without a Home button, swipe down from the top-right corner of the screen instead. Here, tap on the rotation lock icon (which looks like a lock with a circular arrow) to turn it on or off.
You can press Command + Arrow Key to change the iOS simulator screen orientation.
It may be useful for others, I found a way to force the view to launch in landscape mode:
Put this in the viewDidLoad():
let value = UIInterfaceOrientation.landscapeLeft.rawValue UIDevice.current.setValue(value, forKey: "orientation")
and,
override var shouldAutorotate: Bool { return true }
override func viewDidLoad() { super.viewDidLoad() let value = UIInterfaceOrientation.landscapeLeft.rawValue UIDevice.current.setValue(value, forKey: "orientation") } override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .landscapeLeft } override var shouldAutorotate: Bool { return true }
If your view is embedded in a navigation controller, the above alone won't work. You have to cascade up by the following extension after the class definition.
extension UINavigationController { override open var shouldAutorotate: Bool { get { if let visibleVC = visibleViewController { return visibleVC.shouldAutorotate } return super.shouldAutorotate } } override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{ get { if let visibleVC = visibleViewController { return visibleVC.preferredInterfaceOrientationForPresentation } return super.preferredInterfaceOrientationForPresentation } } override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{ get { if let visibleVC = visibleViewController { return visibleVC.supportedInterfaceOrientations } return super.supportedInterfaceOrientations } }}
override func viewDidLoad() { super.viewDidLoad() let value = UIInterfaceOrientation.landscapeLeft.rawValue UIDevice.current.setValue(value, forKey: "orientation") } private func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.landscapeLeft } private func shouldAutorotate() -> Bool { return true }
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