Carlo Villaceran
Carlo Villaceran

Reputation: 31

Swift 3 how to lock orientation

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()

}

Upvotes: 2

Views: 6600

Answers (2)

Apisteftos
Apisteftos

Reputation: 79

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

Upvotes: 6

Moaz Ahmed
Moaz Ahmed

Reputation: 451

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

Upvotes: 1

Related Questions