swift - 快速锁定某些 View 的旋转

标签 swift uiviewcontroller orientation

我将一些 View 设为横向,另一些设为纵向。 我的问题是我不想让它们轮换。 如果页面是横向的,则仅保持横向,对于纵向页面也如此。 我搜索了很多并尝试过:

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

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
} 

但是没有成功

最佳答案

只需通过覆盖获取方向:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.current.orientation.isLandscape && flag{
        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        print("Landscape")
    } else if UIDevice.current.orientation.isPortrait && !flag {
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        print("Portrait")
    }
}

并做这样的事情。

此功能不会锁定您的屏幕,但会将屏幕保持在您想要的方向。

关于swift - 快速锁定某些 View 的旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45545918/

相关文章:

ios - 安全飞地 : update SecAccessControlCreateFlags after key creation

ios - 如何从 Parse 查询中获得超过 1000 个结果?

ios - 从一个类调用另一个类的函数

ios - 弹出 View 时传递参数

ios - Swift:从 xib 以编程方式创建 UIViewController

ios - 在 iOS 8 中的纵向锁定应用程序中将单个 View 旋转为横向

ios - 如何在不重新启动应用程序 swift4 的情况下更改本地化语言?

ios - 点击表格 View 单元格时显示操作表

Android 蓝牙在方向改变后崩溃

ios - iPad 启动图像和方向