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

iOS. How to enable and disable rotation on each UIViewController?

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

示例:

if flag {
   rotateDevice = false
}
else {
   rotateDevice = true
}

我该怎么做?

您只需将 shouldAutorotatesupportedInterfaceOrientations 实施到您的 UIViewController 中。看看this documentation。例如:

override func shouldAutorotate() -> Bool {
    return true
}

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

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

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

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if myFlag {
        return .Landscape
    } else {
        return .All
    }
}

(如果myFlag为真,将允许Landscape方向。否则,将允许所有方向)。

我有答案。 在 AppDelegate 上,如果你旋转设备,按下 viewcontroller,等等。这个函数总是调用

更新 swift 3/4

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

self.restrictRotation为自定义参数。

使用方法:

在 Appdelegate 中:

 var restrictRotation:UIInterfaceOrientationMask = .portrait

在ViewController中:

调用 ViewDidLoad 或 viewWillAppear 方法时。我们会这样改:

(UIApplication.shared.delegate as! AppDelegate).restrictRotation = .all 

然后调用 AppDelegate 上的这个方法。

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask

在 Target -> General -> Deployment Info -> Portrait 中设置设备方向肖像

swift 4

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get {
        return .portrait

    }
}

在 swift5 中,与之前一样,如果您想要允许(避免其他)特定的 UIViewController 以特定方向旋转,您必须在 UIViewController 中覆盖“supportedInterfaceOrientations”class像这样:

class MyViewController:UIViewController {
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }
}

这里有可能的选项:

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

扩展

如果你想区分 iPad 或 iPhone 你可以使用 UIUserInterfaceIdiom:

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

其中:

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
}

针对 AppDelegate:

中最常见请求(iPhone:肖像,iPad:全部)的简单解决方案
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return UIDevice.current.userInterfaceIdiom == .phone ? .portrait : .all
}

发现这是迄今为止最简单的实现,并且适用于特定的视图控制器。将此添加到 viewController class,在 viewDidLoad()

之后
override open var shouldAutorotate: Bool {
        return false
    }

swift 5

override var shouldAutorotate: Bool {
    false
}