简体   繁体   English

如何在 Swift 中以编程方式旋转方向?

[英]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?如何在 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斯威夫特 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:在 viewWillAppear 中的第一个控制器中,我设置了:

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?我该如何解决?

To change orientation programatically in swift 3 , use the following way:要在 swift 3 中以编程方式更改方向,请使用以下方式:

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

You can use the orientations,您可以使用方向,

1. portrait 2. portraitUpsideDown 3. landscapeLeft 4. landscapeRight 1. 纵向 2. 纵向倒置 3. 横向左 4. 横向右

Swift 4:斯威夫特 4:

在此处输入图片说明

Step1:第1步:

In Appdelegate - Declare deviceOrientation var which is portrait by default在 Appdelegate 中 - 声明deviceOrientation var,默认情况下是portrait

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:第2步:

In your certain ViewController write this to change orientation -在你的某个 ViewController 中写这个来改变方向 -

 override func viewWillAppear(_ animated: Bool) {
     appDelegate.deviceOrientation = .landscapeLeft
     let value = UIInterfaceOrientation.landscapeLeft.rawValue
     UIDevice.current.setValue(value, forKey: "orientation")
 } 

You can override UIViewController.supportedInterfaceOrientations() instead of triggering rotation in viewWillAppear您可以覆盖UIViewController.supportedInterfaceOrientations()而不是在viewWillAppear中触发旋转

For Xcode 7对于 Xcode 7

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

For Xcode 6对于 Xcode 6

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

And make sure the desired orientations are enabled in project settings.并确保在项目设置中启用了所需的方向。

在此处输入图片说明

Edit:编辑:

Can modify this to implement any combination of orientations.可以修改它以实现方向的任意组合。

In AppDelegate:在 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 LandscapeRightViewController

View controller is locked in landscapeRight orientation视图控制器被锁定在 LandscapeRight 方向

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.同样,可以修改它以实现您想要的任何锁定方向组合。

swift 5迅捷 5


import UIKit

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM