sazz sazz - 1 year ago 1206
iOS Question

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 {
return false

override func supportedInterfaceOrientations() -> Int {
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.

Answer Source

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.currentDevice().setValue(value, forKey: "orientation")


override func shouldAutorotate() -> Bool {
    return true
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download