Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force landscape mode in one ViewController using Swift

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.

like image 419
sazz Avatar asked Nov 20 '14 10:11

sazz


People also ask

How do I force landscape in iOS?

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.

How do I change the orientation of Xcode?

You can press Command + Arrow Key to change the iOS simulator screen orientation.


2 Answers

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 } 
like image 159
sazz Avatar answered Oct 12 '22 01:10

sazz


Swift 4

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     } }} 


Swift 3

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 } 
like image 28
Manee ios Avatar answered Oct 12 '22 02:10

Manee ios