如何在Swift中编程实现屏幕方向的旋转?

19

我尝试了下一步:

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

关于

viewWillAppear

但它没有效果。在viewWillAppear中如何旋转我的屏幕?

更新

我使用下面的代码来锁定方向:

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)
        }
    }

在我的第一个控制器中的viewWillAppear函数中,我设置了:

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

在我的第二个控制器中:

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

所以,当我从第二个控制器回到第一个控制器时:

我将其向左旋转 - 没有旋转,向右旋转 - 什么都没有发生,再次向左旋转 - 它才开始旋转。

我该如何修复它?

5个回答

42

在 Swift 3 中通过编程方式更改方向,请使用以下方法:

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

您可以使用以下四种方向:

1. 竖屏 2. 倒立竖屏 3. 左横屏 4. 右横屏


2. 倒立竖屏 3. 左横屏 4. 右横屏


2
这不安全吗?如果苹果有一天决定更改“方向”怎么办? - Xavier L.
1
根据苹果文档,@XavierL 这是改变设备方向的方法之一。更多详情请参阅 https://developer.apple.com/documentation/uikit/uideviceorientation。 - Ram Madhavan
1
@XavierL。所以当苹果决定更改时,我们可以进行更改。 - jamryu
1
喜欢这个答案。因为我整天都在搜索,所有其他解决此问题的方法都不起作用。 - jamryu

6

Swift 4:

enter image description here

步骤一:

在 Appdelegate 中声明 deviceOrientation 变量,默认值为 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
    }
}

步骤2:

在您特定的ViewController中编写以下内容以更改方向 -

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

2

swift 5


import UIKit

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


2

您可以重写UIViewController.supportedInterfaceOrientations(),而不是在viewWillAppear中触发旋转。

适用于Xcode 7。

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

针对Xcode 6

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

请确保在项目设置中启用所需的方向。

输入图像描述


它可以工作,但是会崩溃并显示“终止应用程序,原因是未捕获的异常'UIApplicationInvalidInterfaceOrientation',原因是支持的方向与应用程序没有共同的方向,并且[myapp.SecondViewController shouldAutorotate]返回YES”。 - Orkhan Alizade
请确保在应用程序级别中启用横屏。 - Quanlong
我使用的是肖像模式,而不是左横屏,并且我确定这些方向都已启用。 - Orkhan Alizade
我认为“支持的方向与应用程序没有共同的方向”意味着您在ViewController中设置的方向在应用程序级别不可用。 - Quanlong
这些方向支持在我的应用程序级别,请确保我已经选中了这些方向的复选框。 - Orkhan Alizade
显示剩余4条评论

2

编辑:

可以修改此代码以实现任何方向的组合。

在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
    }
}

横屏左侧视图控制器

该视图控制器被锁定在横屏左侧方向。

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)
}

横屏右侧视图控制器

该视图控制器被锁定在横向右侧方向。

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)
}

所有其他视图控制器都被锁定为纵向方向。同样,您可以修改此设置以实现您想要的任何锁定方向组合。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接