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

4dc9hkyq  于 2022-11-23  发布在  Swift
关注(0)|答案(8)|浏览(385)

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

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

我怎么能那样做呢?

ldfqzlk8

ldfqzlk81#

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

swift 3/4的更新

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

self.restrictRotation是自定义参数。

使用方法:

  • 在应用委托中:*
var restrictRotation:UIInterfaceOrientationMask = .portrait
  • 在视图控制器中:*

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

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

然后调用AppDelegate上的此方法。

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

sqougxex2#

你只需要在你的UIViewController中实现shouldAutorotatesupportedInterfaceOrientations

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

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

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

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

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

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

rwqw0loc

rwqw0loc3#

雨燕4

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    get {
        return .portrait

    }
}
i1icjdpr

i1icjdpr4#

在swift 5中,如前所述,如果您希望允许(避免其他)特定UIViewController以特定方向旋转,则必须覆盖UIViewController类中的“supportedInterfaceOrientations”,如下所示:

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

kmb7vmvb5#

最常见请求的简单解决方案(iPhone:纵向,iPad:所有)在您的AppDelegate

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

lrl1mhuk6#

发现这是迄今为止最简单的实现,并且适合在特定的视图控制器上使用。

override open var shouldAutorotate: Bool {
        return false
    }
9gm1akwq

9gm1akwq7#

雨燕5

override var shouldAutorotate: Bool {
    false
}
eiee3dmh

eiee3dmh8#

在“目标”-〉“常规”-〉“部署信息”-〉“纵向”中设置设备纵向方向

相关问题