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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38969419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:55:46  来源:igfitidea点击:

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

iosswiftswift2

提问by Bao Tuan Diep

I have an UIViewController, I want to disable or enable rotation of the screen in different scenarios

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

Example:

例子:

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

How can I do that?

我怎样才能做到这一点?

回答by Bao Tuan Diep

I have the answer. On AppDelegate, if you rotate device, push viewcontroller, etc. This function always call

我有答案。On AppDelegate,如果你旋转设备,推视图控制器等。这个函数总是调用

Update for swift 3/4

快速 3/4更新

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

self.restrictRotationis a custom parameter.

self.restrictRotation是自定义参数。

How to use:

如何使用:

In Appdelegate:

在 Appdelegate 中:

 var restrictRotation:UIInterfaceOrientationMask = .portrait

In ViewController:

在视图控制器中:

When method ViewDidLoad or viewWillAppear is called. We will change like this:

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

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

and then this method on AppDelegate will be called.

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

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

回答by Julien Quere

You simply have to implement shouldAutorotateand supportedInterfaceOrientationsinto your UIViewController. Take a look at this documentation. For example:

您只需实现shouldAutorotatesupportedInterfaceOrientations进入您的UIViewController. 看看这个文档。例如:

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

You can also specify which orientations are available. Here is an example with only portraits orientations:

您还可以指定哪些方向可用。这是一个只有纵向方向的示例:

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

Edit: If you want to support different orientation regarding a flag, you just have to do something like this:

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

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

(If myFlagis true, it will allow Landscapeorientation. Otherwise, it will allow all orientations).

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

回答by Baryon Lee

swift 4

迅捷 4

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get {
        return .portrait

    }
}

回答by Reimond Hill

In swift 5, as from previous, if you want to allow (avoid others) a particular UIViewController to rotate in a certain orientation you have to override "supportedInterfaceOrientations" inside your UIViewController class like so:

在 swift 5 中,和以前一样,如果您想允许(避免其他人)特定的 UIViewController 以特定方向旋转,您必须像这样覆盖 UIViewController 类中的“supportedInterfaceOrientations”:

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

Here there are the possible options:

这里有可能的选项:

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

Extended

扩展

If you want to be able to distinguish between iPad or iPhone you could use UIUserInterfaceIdiom:

如果您希望能够区分 iPad 或 iPhone,您可以使用 UIUserInterfaceIdiom:

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

where:

在哪里:

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
}

回答by catanore

An easy solution for the most common request (iPhone: portrait, iPad: all) in your AppDelegate:

最常见请求(iPhone:纵向,iPad:全部)的简单解决方案AppDelegate

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

回答by NGR

set device orientarion portrait in Target -> General -> Deployment Info -> Portrait

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

enter image description here

在此处输入图片说明