How to rotate orientation programmatically in Swift?

后端 未结 5 1252
有刺的猬
有刺的猬 2020-12-11 15:53

I tried the next:

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

on

         


        
相关标签:
5条回答
  • 2020-12-11 16:10

    swift 5

    
    import UIKit
    
    extension UIViewController {
        class func setUIInterfaceOrientation(_ value: UIInterfaceOrientation) {
            UIDevice.current.setValue(value.rawValue, forKey: "orientation")
        }
    }
    
    
    0 讨论(0)
  • 2020-12-11 16:13

    Swift 4:

    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")
     } 
    
    0 讨论(0)
  • 2020-12-11 16:20

    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.

    0 讨论(0)
  • 2020-12-11 16:23

    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.

    0 讨论(0)
  • 2020-12-11 16:24

    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

    0 讨论(0)
提交回复
热议问题