iOS版.如何在每个UIViewController上启用和禁用旋转?

Bao*_*iep 27 ios swift swift2

我有一个UIViewController,我想在不同的场景中禁用或启用屏幕旋转

例:

if flag {
   rotateDevice = false
}
else {
   rotateDevice = true
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

Bao*_*iep 42

我有答案.在AppDelegate,如果您旋转设备,请按viewcontroller等.此功能始终调用

更新swift 3/4

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
{
    return self.restrictRotation
}
Run Code Online (Sandbox Code Playgroud)

self.restrictRotation 是一个自定义参数.

如何使用:

在Appdelegate:

 var restrictRotation:UIInterfaceOrientationMask = .portrait
Run Code Online (Sandbox Code Playgroud)

在ViewController中:

调用方法ViewDidLoad或viewWillAppear时.我们将改变如下:

(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all 
Run Code Online (Sandbox Code Playgroud)

然后将调用AppDelegate上的此方法.

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask
Run Code Online (Sandbox Code Playgroud)

  • 这里的“self.restrictRotation”是什么?在哪里清除? (2认同)
  • 我按照这个方法做了,但是有问题。如果您在“.all”视图控制器上处于横向状态,然后转到一个新的视图控制器(在“viewWillAppear”中将正确的代码设置为仅允许“.portrait”),则该新视图控制器将保留在景观。我会继续寻找修复方法。 (2认同)

Jul*_*ere 24

你只需要实现shouldAutorotatesupportedInterfaceOrientations进入你的UIViewController.看一下这个文档.例如:

override func shouldAutorotate() -> Bool {
    return true
}
Run Code Online (Sandbox Code Playgroud)

您还可以指定可用的方向.这是一个只有肖像方向的例子:

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

编辑:如果你想支持关于旗帜的不同方向,你只需要做这样的事情:

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

(如果myFlag是,它将允许Landscape方向.否则,它将允许所有方向).

  • @Moondra,swift 4 将它们定义为计算变量而不是函数。使用 Xcode 的代码完成建议 (3认同)
  • 在 Swift 4 中,如果我将函数放在视图控制器中,我会得到“方法不会覆盖其超类中的任何方法”。 (2认同)

Bar*_*Lee 14

迅速4

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get {
        return .portrait

    }
}
Run Code Online (Sandbox Code Playgroud)

  • 添加扩展将使答案清楚.不仅是OP,还有未来的用户也遇到过这个问题. (3认同)

cat*_*ore 10

一个简单的解决方案,可满足您的最常见要求(iPhone:纵向,iPad:全部)AppDelegate

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
}
Run Code Online (Sandbox Code Playgroud)


Rei*_*ill 6

在Swift 5中,如前所述,如果要允许(避免其他)特定的UIViewController沿某个方向旋转,则必须重写UIViewController类中的“ supportedInterfaceOrientations”,如下所示:

class MyViewController:UIViewController{
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
        get{
            return .portrait
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这里有可能的选项:

public struct UIInterfaceOrientationMask : OptionSet {

    public init(rawValue: UInt)


    public static var portrait: UIInterfaceOrientationMask { get }

    public static var landscapeLeft: UIInterfaceOrientationMask { get }

    public static var landscapeRight: UIInterfaceOrientationMask { get }

    public static var portraitUpsideDown: UIInterfaceOrientationMask { get }

    public static var landscape: UIInterfaceOrientationMask { get }

    public static var all: UIInterfaceOrientationMask { get }

    public static var allButUpsideDown: UIInterfaceOrientationMask { get }
}
Run Code Online (Sandbox Code Playgroud)

扩展的

如果要区分iPad或iPhone,可以使用UIUserInterfaceIdiom:

override var supportedInterfaceOrientations: UIInterfaceOrientationMask{
    get{
        return UIDevice.current.userInterfaceIdiom == .phone ? [.portrait, . portraitUpsideDown]:.all //OBS -> You can also return an array
    }
}
Run Code Online (Sandbox Code Playgroud)

哪里:

public enum UIUserInterfaceIdiom : Int {


    case unspecified

    @available(iOS 3.2, *)
    case phone // iPhone and iPod touch style UI

    @available(iOS 3.2, *)
    case pad // iPad style UI

    @available(iOS 9.0, *)
    case tv // Apple TV style UI

    @available(iOS 9.0, *)
    case carPlay // CarPlay style UI
}
Run Code Online (Sandbox Code Playgroud)