Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock orientation of one view controller to portrait mode only in Swift

Since my app got support for all orientation. I would like to lock only portrait mode to specific UIViewController.

e.g. assume it was Tabbed Application and when SignIn View appear modally, I only want that SignIn View to the portrait mode only no matter how the user rotate the device or how the current device orientation will be

like image 924
Thiha Aung Avatar asked Mar 09 '15 09:03

Thiha Aung


People also ask

How do I lock my screen in portrait position?

If you don't want the screen to switch between portrait and landscape when you move the device, you can lock the screen orientation. To do this, swipe down from the right side of the top panel. Hold the device in the orientation in which you want it locked. On the drop-down menu, touch the “Auto Rotate” button.

How do I change the orientation of Xcode?

You can press Command + Arrow Key to change the iOS simulator screen orientation.


2 Answers

Things can get quite messy when you have a complicated view hierarchy, like having multiple navigation controllers and/or tab view controllers.

This implementation puts it on the individual view controllers to set when they would like to lock orientations, instead of relying on the App Delegate to find them by iterating through subviews.

Swift 3, 4, 5

In AppDelegate:

/// set orientations you want to be allowed in this property by default var orientationLock = UIInterfaceOrientationMask.all  func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {         return self.orientationLock } 

In some other global struct or helper class, here I created AppUtility:

struct AppUtility {      static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {              if let delegate = UIApplication.shared.delegate as? AppDelegate {             delegate.orientationLock = orientation         }     }      /// OPTIONAL Added method to adjust lock and rotate to the desired orientation     static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {             self.lockOrientation(orientation)              UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")         UINavigationController.attemptRotationToDeviceOrientation()     }  } 

Then in the desired ViewController you want to lock orientations:

 override func viewWillAppear(_ animated: Bool) {     super.viewWillAppear(animated)          AppUtility.lockOrientation(.portrait)     // Or to rotate and lock     // AppUtility.lockOrientation(.portrait, andRotateTo: .portrait)      }  override func viewWillDisappear(_ animated: Bool) {     super.viewWillDisappear(animated)          // Don't forget to reset when view is being removed     AppUtility.lockOrientation(.all) } 

If iPad or Universal App

Make sure that "Requires full screen" is checked in Target Settings -> General -> Deployment Info. supportedInterfaceOrientationsFor delegate will not get called if that is not checked. enter image description here

like image 152
bmjohns Avatar answered Sep 19 '22 11:09

bmjohns


Swift 4

enter image description here AppDelegate

var orientationLock = UIInterfaceOrientationMask.all      func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {     return self.orientationLock } struct AppUtility {     static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {         if let delegate = UIApplication.shared.delegate as? AppDelegate {             delegate.orientationLock = orientation         }     }              static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {         self.lockOrientation(orientation)         UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")     } } 

Your ViewController Add the following line if you need only portrait orientation. you have to apply this to all ViewController need to display portrait mode.

override func viewWillAppear(_ animated: Bool) {     AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.portrait, andRotateTo: UIInterfaceOrientation.portrait) } 

and that will make screen orientation for others Viewcontroller according to device physical orientation.

override func viewWillDisappear(_ animated: Bool) {     AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.all) } 
like image 33
Nahid Raihan Avatar answered Sep 20 '22 11:09

Nahid Raihan