18

I tried the next:

UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")

on

viewWillAppear

but it does not work. How can I rotate my screen on viewWillAppear?

UPDATE

I use the next code for locking an orientation:

var shouldRotate = true
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
    if shouldRotate == true {
        return Int(UIInterfaceOrientationMask.Portrait.rawValue)
    } else {
        return Int(UIInterfaceOrientationMask.LandscapeLeft.rawValue)
    }
}

Swift 4.0

  private func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
        if shouldRotate == true {
            return Int(UIInterfaceOrientationMask.portrait.rawValue)
        } else {
            return Int(UIInterfaceOrientationMask.landscapeLeft.rawValue)
        }
    }

and in my FIRST controller in viewWillAppear I set:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
    appDelegate.shouldRotate = true
}

in my SECOND controller:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
    appDelegate.shouldRotate = false
}

so, when I back from SECOND controller to the FIRST:

I rotate it to left - does not rotate, rotate back to right - nothing, rotate to left, again - it rotates.

How can I fix it?

Rohit Pathak
  • 359
  • 4
  • 16
Orkhan Alizade
  • 7,379
  • 14
  • 40
  • 79

5 Answers5

40

To change orientation programatically in swift 3, use the following way:

let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")

You can use the orientations,

1. portrait 2. portraitUpsideDown 3. landscapeLeft 4. landscapeRight

Ram Madhavan
  • 2,362
  • 1
  • 17
  • 20
  • 2
    Isn't this unsafe? What if Apple decides to change "orientation" one day? – Xavier L. Mar 30 '18 at 11:45
  • 1
    @XavierL. according to apple documentation, this is one of the method to change the orientation of device. for more details check https://developer.apple.com/documentation/uikit/uideviceorientation – Ram Madhavan Apr 06 '18 at 03:44
  • 1
    @XavierL. So we can change it when Apple decides to change it. – jamryu Feb 02 '21 at 05:09
  • 1
    Love this answer. Because I searched all day and all other solutions regarding this problem was not working. – jamryu Feb 02 '21 at 05:10
6

Swift 4:

enter image description here

Step1:

In Appdelegate - Declare deviceOrientation var which is portrait by default

import UIKit
let appDelegate = UIApplication.shared.delegate as! AppDelegate
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    var deviceOrientation = UIInterfaceOrientationMask.portrait
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return deviceOrientation
    }
}

Step 2:

In your certain ViewController write this to change orientation -

 override func viewWillAppear(_ animated: Bool) {
     appDelegate.deviceOrientation = .landscapeLeft
     let value = UIInterfaceOrientation.landscapeLeft.rawValue
     UIDevice.current.setValue(value, forKey: "orientation")
 } 
Jack
  • 13,571
  • 6
  • 76
  • 98
2

You can override UIViewController.supportedInterfaceOrientations() instead of triggering rotation in viewWillAppear

For Xcode 7

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Landscape
}

For Xcode 6

override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}

And make sure the desired orientations are enabled in project settings.

enter image description here

Quanlong
  • 24,028
  • 16
  • 69
  • 79
2

Edit:

Can modify this to implement any combination of orientations.

In AppDelegate:

internal var viewControllerOrientation = 0
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if viewControllerOrientation == 0 {
        return .portrait
    } else if viewControllerOrientation == 1 {
      return .landscapeLeft
    } else if viewControllerOrientation == 2 {
      return .landscapeRight
    }
}

LandscapeLeftViewController

View controller is locked in landscapeLeft orientation

override func viewDidAppear(_ animated: Bool) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.viewControllerOrientation = 1
}

@IBAction func newVCBtnWasPressed(_ sender: Any) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.viewControllerOrientation = 0

    // go to different view controller method, such as:
    dismiss(animated: true, completion: nil)
}

LandscapeRightViewController

View controller is locked in landscapeRight orientation

override func viewDidAppear(_ animated: Bool) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.viewControllerOrientation = 2
}

@IBAction func newVCBtnWasPressed(_ sender: Any) {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.viewControllerOrientation = 0

    // go to different view controller method, such as:
    dismiss(animated: true, completion: nil)
}

All other view controllers are locked in portrait orientation. Again, can modify this to implement any combination of locked orientations you want.

Damon
  • 21
  • 4
2

swift 5


import UIKit

extension UIViewController {
    class func setUIInterfaceOrientation(_ value: UIInterfaceOrientation) {
        UIDevice.current.setValue(value.rawValue, forKey: "orientation")
    }
}

hectorsvill
  • 681
  • 8
  • 7