禁用 tabBarController 中项目的旋转

Disable rotation for item in tabBarController

我有一个 tabBarController,它有 4 个项目。其中之一是我用 AVCaptureSession 实现的相机(条形码扫描仪)。因此,如果您点击 "scanner" 选项卡,将自动显示相机屏幕。

问题是我无法禁用 tabBarController 的单个项目的自动旋转。所以,当你旋转设备时,相机的屏幕也会旋转,非常奇怪。

我试过了:

override func shouldAutorotate() -> Bool {

    return false

}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

    return .Portrait

}

但没有任何效果。

在您的 AppDelegate 中添加以下内容

var shouldSupportAllOrientation = false
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    if (shouldSupportAllOrientation == true){
        return UIInterfaceOrientationMask.All
    }
    return UIInterfaceOrientationMask.Portrait
}

然后转到每个视图并在 viewWillAppear

中添加以下内容
let appdelegate = UIApplication.sharedApplication().delegate as! AppDelegate
// false = only portrait
// true = all orientations
appdelegate.shouldSupportAllOrientation = false

更新 要在从横向切换到纵向时锁定屏幕,只需在 viewWillAppear 中添加此代码即可。

let value = UIInterfaceOrientation.Portrait.rawValue
UIDevice.currentDevice().setValue(value, forKey: "orientation")