2

I am trying to lock my viewController to just portrait orientation without having to rely on Deployment Info but my following code isn't working

override var shouldAutorotate: Bool {
    return false
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
}

 override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
    return UIInterfaceOrientation.portrait
}

override func viewDidLoad() {
    super.viewDidLoad()

}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • 2
    why not rely on that info? it's not a good way to try to be smarter than xcode – Lu_ Nov 03 '16 at 06:50
  • 1
    I copied and paste your code and it does lock the viewController to *portrait* mode, `override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return UIInterfaceOrientationMask.portrait }` should be enough for your case. – Ahmad F Nov 03 '16 at 07:24
  • @Carlo plz have a look here :- https://stackoverflow.com/questions/31758706/how-to-force-or-disable-interface-orientation-for-some-but-not-all-uiviewcontrol/31801804#31801804 – Anand Suthar Aug 22 '17 at 06:11

2 Answers2

6

You can try this in the AppDelegate. This will lock the orientation for all your viewControllers

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask(rawValue: UIInterfaceOrientationMask.portrait.rawValue)
}
Douwe de Haan
  • 6,247
  • 1
  • 30
  • 45
Apisteftos
  • 79
  • 1
  • 3
1

Make sure you put your code in parent view Controller otherwise it may not work, for example, in the navigation controller view controller class.

Moaz Ahmed
  • 441
  • 2
  • 8