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!
Asked
Active
Viewed 625 times
4
-
1possible duplicate http://stackoverflow.com/questions/26357162/how-to-force-view-controller-orientation-in-ios-8 – Gagan_iOS Oct 30 '15 at 20:35
-
1@Gagan_iOS none of them work, I want the solution for iOS 9, the solutions that are present in the link that you posted do not work for iOS 9. – pogbamessi Nov 02 '15 at 16:29
1 Answers
2
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
}

bobski
- 152
- 3
- 14
-
It'd be more appropriate if the first code snippet goes to ViewWillAppear. – jamryu Feb 01 '21 at 22:27