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

Ork*_*ade 8 orientation ios swift

我尝试了下一个:

UIDevice.currentDevice().setValue(UIInterfaceOrientation.Portrait.rawValue, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)

viewWillAppear
Run Code Online (Sandbox Code Playgroud)

但它不起作用.如何在viewWillAppear上旋转屏幕?

UPDATE

我使用下一个代码来锁定方向:

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)
    }
}
Run Code Online (Sandbox Code Playgroud)

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)
        }
    }
Run Code Online (Sandbox Code Playgroud)

在我的第一个控制器viewWillAppear我设置:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
    appDelegate.shouldRotate = true
}
Run Code Online (Sandbox Code Playgroud)

在我的SECOND控制器中:

if let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate {
    appDelegate.shouldRotate = false
}
Run Code Online (Sandbox Code Playgroud)

所以,当我从SECOND控制器回到FIRST:

我向左旋转 - 不旋转,向后旋转 - 没有,向左旋转,再旋转 - 旋转.

我该如何解决?

Ram*_*van 20

要在swift 3中以编程方式更改方向,请使用以下方法:

let value = UIInterfaceOrientation.landscapeRight.rawValue
UIDevice.current.setValue(value, forKey: "orientation")
Run Code Online (Sandbox Code Playgroud)

你可以使用方向,

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

  • 你救我了男人.他们现在不会解雇我! (3认同)
  • 这不是不安全吗?如果苹果决定有一天改变“方向”怎么办? (2认同)

Jac*_*ack 5

斯威夫特 4:

在此处输入图片说明

第1步:

在 Appdelegate 中 - 声明默认情况下的deviceOrientationvarportrait

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
    }
}
Run Code Online (Sandbox Code Playgroud)

第2步:

在你的某个 ViewController 中写这个来改变方向 -

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


Qua*_*ong 2

您可以覆盖UIViewController.supportedInterfaceOrientations()而不是触发旋转viewWillAppear

对于 Xcode 7

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.Landscape
}
Run Code Online (Sandbox Code Playgroud)

对于 Xcode 6

override func supportedInterfaceOrientations() -> Int {
    return Int(UIInterfaceOrientationMask.Landscape.rawValue)
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述