0

I want to present a UIView controller already in landscape mode and disable any other rotations for this UIViewController. I tried the following, but the user can still rotate out of landscape.

override func viewDidLoad() {
    super.viewDidLoad()
    applyLandscapeAndLock()
    build()
    // Do any additional setup after loading the view.
}

override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
    return UIInterfaceOrientation.LandscapeLeft
}

override func shouldAutorotate() -> Bool {
    return false
}

override func supportedInterfaceOrientations() -> Int {
    return UIInterfaceOrientation.LandscapeLeft.rawValue
}

func applyLandscapeAndLock() {
    let value = UIInterfaceOrientation.LandscapeLeft.rawValue
    UIDevice.currentDevice().setValue(value, forKey: "orientation")
}
Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
William Falcon
  • 9,813
  • 14
  • 67
  • 110
  • This answer should help - http://stackoverflow.com/a/28220616/46297 – lostInTransit Jun 04 '15 at 12:28
  • 1
    possible duplicate of [How to force view controller orientation in iOS 8?](http://stackoverflow.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8) – lostInTransit Jun 04 '15 at 12:28
  • I don't find anything wrong with your code I tested myself and it works fine, where you are testing this code? – Victor Sigler Jun 04 '15 at 13:14
  • if that viewController is a child from a navigation controller, you have to set so the navigation controller passes the rotation callbacks to that viewController, normally, it doesnt. – Erakk Jun 04 '15 at 16:43

0 Answers0