如何更改设备方向

How to change device orientation

我想更改我的应用程序的方向。我唯一找到的是:

self.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT)

但它说找不到 UIA_Device_Orientation_Portrait。 谁能告诉我为什么(也许我必须导入一些东西)或者我还能如何将方向设置为 Portrai 模式? 谢谢

如果你想手动旋转,这样做:

UIDevice.currentDevice().setValue(UIInterfaceOrientation.LandscapeLeft.rawValue, forKey: "orientation")

如果自动执行,请执行以下操作:

// ViewController.swift
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    return .All
}

并且如果您的视图控制器位于 navigation/tabbar 控制器中,请设置该 navigation/tabbar 控制器。

更新: 这是如何控制自动旋转

override func shouldAutorotate() -> Bool {
    return false // adjust this
}

确保您的主要 plist 已调整为允许多个方向。您可以在 plist.

Supported interface orientations 标签下找到它