Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate orientation programmatically in Swift?

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?

like image 733
Orkhan Alizade Avatar asked Sep 02 '15 05:09

Orkhan Alizade


4 Answers

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

like image 73
Ram Madhavan Avatar answered Nov 11 '22 17:11

Ram Madhavan


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")
 } 
like image 23
Jack Avatar answered Nov 11 '22 19:11

Jack


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

like image 2
Quanlong Avatar answered Nov 11 '22 19:11

Quanlong


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.

like image 2
Damon Avatar answered Nov 11 '22 17:11

Damon