pogbamessi
pogbamessi

Reputation: 333

Enable portrait and landscape for one view controller and only portrait for other view controllers in swift?

I have 10 view controllers in my app and i want only one view controller to be viewed in portrait and landscape mode and the rest should only be viewed in portrait mode. Note: I am using UINavigationController. Any help is appreciated!

Upvotes: 4

Views: 640

Answers (1)

bobski
bobski

Reputation: 172

I had the same problem, but found a solution:

First, allow Landscape mode for your App in the basic settings. Then modify your AppDelegate:

internal var shouldRotate = false

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {

    return shouldRotate ? .allButUpsideDown : .portrait

}

For every Controller, where landscape mode should be allowed, add the following code to the viewDidLoad() :

let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.shouldRotate = true

That allows for the entire application to be shown in landscape mode. But it should be only in this controller, so add following code in viewWillDissappear() to set the shouldRotate var in the AppDelegate to false:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(true)

    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.shouldRotate = false

}

Upvotes: 2

Related Questions