Setting device orientation in Swift iOS

I am working on a fast app for iPhone. My application has a modal view that I want to see only in portrait mode.

My question is: how to programmatically make the phone not allow rotation? In other words, I'm looking for code that will not allow me to display a modal view in landscape mode (enabling vertical rotation lock).

This is just 1 modal view, so I cannot turn off rotation for the whole application, otherwise I would just turn off rotation at all.

I found the code in my research here. But this is in the C lens, in case this helps. Thank!

+58
rotation ios8 swift
03 Sep '14 at 19:11
source share
17 answers

You can insert these methods into the ViewController of each view, which should be a portrait:

override func shouldAutorotate() -> Bool { return false } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait } 
+48
Sep 03 '14 at 20:54 on
source share

Hello for LandscapeLeft and LandscapeRight (Update Swift 2.0)

enter image description here And you have it in the information

enter image description here

And UIController

 override func shouldAutorotate() -> Bool { return true } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight] } 

For PortraitUpsideDown and Portrait use this enter image description here

 override func shouldAutorotate() -> Bool { if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft || UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) { return false } else { return true } } override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown] } 

Message from France, Merry Christmas!

Edit:

Another solution:

 extension UINavigationController { public override func shouldAutorotate() -> Bool { if visibleViewController is MyViewController { return true // rotation } else { return false // no rotation } } public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return (visibleViewController?.supportedInterfaceOrientations())! } } 
+68
Dec 24 '14 at 23:52
source share

Swift 3

Rotation orientation is more complicated if the view controller is built into the UINavigationController or UITabBarController, the navigation controller or tab bar takes precedence and decides on autorotation and supported orientation.

Use the following extensions in the UINavigationController and UITabBarController so that the view controllers built into one of these controllers make decisions:

UINavigationController Extension

 extension UINavigationController { override open var shouldAutorotate: Bool { get { if let visibleVC = visibleViewController { return visibleVC.shouldAutorotate } return super.shouldAutorotate } } override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{ get { if let visibleVC = visibleViewController { return visibleVC.preferredInterfaceOrientationForPresentation } return super.preferredInterfaceOrientationForPresentation } } override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{ get { if let visibleVC = visibleViewController { return visibleVC.supportedInterfaceOrientations } return super.supportedInterfaceOrientations } }} 

UITabBarController Extension

 extension UITabBarController { override open var shouldAutorotate: Bool { get { if let selectedVC = selectedViewController{ return selectedVC.shouldAutorotate } return super.shouldAutorotate } } override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{ get { if let selectedVC = selectedViewController{ return selectedVC.preferredInterfaceOrientationForPresentation } return super.preferredInterfaceOrientationForPresentation } } override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{ get { if let selectedVC = selectedViewController{ return selectedVC.supportedInterfaceOrientations } return super.supportedInterfaceOrientations } }} 

Now you can override the supported InterfaceOrientations, shouldAutoRotate and preferredInterfaceOrientationForPresentation in the view controller that you want to block, otherwise you can leave the overrides in other view controllers that you want to inherit by default for the orientation specified in your application.

Orientation Lock

 class YourViewController: UIViewController { open override var supportedInterfaceOrientations: UIInterfaceOrientationMask{ get { return .portrait } }} 

Disable rotation

  class YourViewController: UIViewController { open override var shouldAutorotate: Bool { get { return false } }} 

Change preferred presentation orientation for presentation

 class YourViewController: UIViewController { open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{ get { return .portrait } }} 
+39
Sep 24 '16 at 9:11
source share

The above code may not work because of the possibility if your view controller belongs to the navigation controller. If so, then it must obey the rules of the navigation controller, even if it has different orientation rules. A better approach would be to let the view controller decide for itself, and the navigation controller would use the solution of the top controller.

We can support both fixation in the current orientation and autorotation to block a specific orientation using this general extension on the UINavigationController: -:

 extension UINavigationController { public override func shouldAutorotate() -> Bool { return visibleViewController.shouldAutorotate() } public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return (visibleViewController?.supportedInterfaceOrientations())! } } 

Now in your view controller we can

 class ViewController: UIViewController { // MARK: Autoroate configuration override func shouldAutorotate() -> Bool { if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait || UIDevice.currentDevice().orientation == UIDeviceOrientation.PortraitUpsideDown || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) { return true } else { return false } } override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Portrait.rawValue) | Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue) } } 

Hope this helps. Thanks

+18
Jun 26 '15 at 13:05
source share

This will disable view autorotation:

 override func shouldAutorotate() -> Bool { return false; } 

Update

 override func shouldAutorotate() -> Bool { if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft || UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) { return false; } else { return true; } } 

If the application is in landscape mode, and you show the view that should be displayed in portrait mode, this will allow the application to change the orientation to the portrait (of course, when the device is rotated to this orientation).

+11
03 Sep '14 at 20:32
source share

If someone wants an answer, I think I got it. Try the following:

  • Go to your .plist file and check all orientations.
  • In the view controller you want to force the orientation, add the following code:
 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait.toRaw().hashValue | UIInterfaceOrientationMask.PortraitUpsideDown.toRaw().hashValue } 

Hope this helps!

EDIT:

To force rotation, use the following code:

 let value = UIInterfaceOrientation.LandscapeRight.rawValue UIDevice.currentDevice().setValue(value, forKey: "orientation") 

It works for iOS 7 and 8!

+11
Sep 30 '14 at 9:26
source share

enter image description here

 Go to your pList and add or remove the following as per your requirement: "Supported Interface Orientations" - Array "Portrait (bottom home button)" - String "Portrait (top home button)" - String "Supported Interface Orientations (iPad)" - Array "Portrait (bottom home button)" - String "Portrait (top home button)" - String "Landscape (left home button)" - String "Landscape (right home button)" - String 

Note. This method allows you to rotate for the entire application.

OR

Create a ParentViewController for the UIViewControllers in the project (inheritance method).

 // UIappViewController.swift import UIKit class UIappViewController: UIViewController { super.viewDidLoad() } //Making methods to lock Device orientation. override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { return UIInterfaceOrientationMask.Portrait } override func shouldAutorotate() -> Bool { return false } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 

Bind each view controller controller as a UIappViewController.

 // LoginViewController.swift import UIKit import Foundation class LoginViewController: UIappViewController{ override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } 
+9
Oct 22 '15 at 12:46
source share

For Swift 3, iOS 10

 override open var shouldAutorotate: Bool { return false } override open var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait } override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { return .portrait } 

However, the error with setting shouldAutorotate does not currently work on iOS 9.

+8
Nov 18 '16 at 22:46
source share

In the info.plist file, change the orientation you want in the "supported interface orientation".

In fast mode, files-> info.plist-> are supported, which support the orientation of the interface.

+6
Dec 16 '14 at 5:40
source share

I fought all morning to get ONLY left / right terrain, supported properly. I found something really annoying; although the General tab allows you to deselect Portrait for device orientation, you must edit the layer itself to disable portrait orientation orientation and PortraitUpsideDown INTERFACE is the last key in plist: “Supported interface orientations”.

Another thing is that, it seems, you should use the "masking" versions of the enumerations (for example, UIInterfaceOrientationMask.LandscapeLeft), and not just the orientation one. The code that made it work for me (in my main viewController):

 override func shouldAutorotate() -> Bool { return true } override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue) | Int(UIInterfaceOrientationMask.LandscapeRight.rawValue) } 

Creating this combination of plist and code changes is the only way I was able to get it to work properly.

+4
Dec 03 '14 at 19:08
source share

Faster version:

 override func shouldAutorotate() -> Bool { switch UIDevice.currentDevice().orientation { case .Portrait, .PortraitUpsideDown, .Unknown: return true default: return false } } override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Portrait.rawValue) | Int(UIInterfaceOrientationMask.PortraitUpsideDown.rawValue) } 

UINavigationController by Vivek Parihar

 extension UINavigationController { public override func shouldAutorotate() -> Bool { return visibleViewController.shouldAutorotate() } } 
+4
Aug 19 '15 at 9:01
source share

// Swift 2

 override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask { let orientation: UIInterfaceOrientationMask = [UIInterfaceOrientationMask.Portrait, UIInterfaceOrientationMask.PortraitUpsideDown] return orientation } 
+3
Dec 01 '15 at 19:46
source share

Two suggestions with @Vivek Parihar solution:

  • If we represent any viewController, we must check nil for the "visibleViewController" in the navigationController extension

     extension UINavigationController { public override func shouldAutorotate() -> Bool { var shouldAutorotate = false if visibleViewController != nil { shouldAutorotate = visibleViewController.shouldAutorotate() } return shouldAutorotate } public override func supportedInterfaceOrientations() -> Int { return visibleViewController.supportedInterfaceOrientations() } } 
  • If we use any action file for presentation, and the user rotates at the top, your action sheet will open from the top of the screen: P, to solve this problem, we need to take only a portrait

     override func shouldAutorotate() -> Bool { if (UIDevice.currentDevice().orientation == UIDeviceOrientation.Portrait || UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) { return true } else { return false } 

    }

     override func supportedInterfaceOrientations() -> Int { return Int(UIInterfaceOrientationMask.Portrait.rawValue) } 
+2
Sep 16 '15 at 15:15
source share

Swift 2.2

  func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask { if self.window?.rootViewController?.presentedViewController is SignatureLandscapeViewController { let secondController = self.window!.rootViewController!.presentedViewController as! SignatureLandscapeViewController if secondController.isPresented { return UIInterfaceOrientationMask.LandscapeLeft; } else { return UIInterfaceOrientationMask.Portrait; } } else { return UIInterfaceOrientationMask.Portrait; } } 
0
May 12 '16 at 1:47
source share

My humble contribution (Xcode 8, Swift 3):

 func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask { if let rootViewController = self.topViewControllerWithRootViewController(rootViewController: window?.rootViewController) { if (rootViewController.responds(to: Selector(("canRotate")))) { // Unlock landscape view orientations for this view controller return .allButUpsideDown; } } return .portrait; } private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? { if (rootViewController == nil) { return nil } if (rootViewController.isKind(of: (UITabBarController).self)) { return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UITabBarController).selectedViewController) } else if (rootViewController.isKind(of:(UINavigationController).self)) { return topViewControllerWithRootViewController(rootViewController: (rootViewController as! UINavigationController).visibleViewController) } else if (rootViewController.presentedViewController != nil) { return topViewControllerWithRootViewController(rootViewController: rootViewController.presentedViewController) } return rootViewController } 

... on AppDelegate. All loans for Gandhi Mena: http://www.jairobjunior.com/blog/2016/03/05/how-to-rotate-only-one-view-controller-to-landscape-in-ios-slash-swift/

0
Feb 04 '17 at 3:14
source share

From ios 10.0 we need set { self.orientations = newValue } to set the orientation, make sure that the landscape property is enabled in your project.

 private var orientations = UIInterfaceOrientationMask.landscapeLeft override var supportedInterfaceOrientations : UIInterfaceOrientationMask { get { return self.orientations } set { self.orientations = newValue } } 
0
Jun. 19 '17 at 4:07 on
source share

Swift 4:

The simplest answer, in my case, is to make sure that just viewing a textbook on board was only portrait:

 extension myViewController { //manage rotation for this viewcontroller override open var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait } } 

Eezy-peezy.

0
Jun 05 '18 at 12:35
source share



All Articles