ios - 只在某些特定的 ViewController 中启用横向

标签 ios swift

我的应用程序有许多不同的ViewController,我想允许某些特定的ViewController可以旋转横向。我尝试过使用supportedInterfaceOrientations或shouldAutorotate之类的方法,但它不起作用。 还有人有其他想法吗?

最佳答案

仅在 ViewController 中更新是不够的,您必须在 AppDelegate 中更新更多内容。

应用程序委托(delegate)

var enableAllOrientation = false
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if enableAllOrientation {
        return .all
    }
    return .portrait
}

任何你想要支持景观的ViewController,只需实现:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.enableAllOrientation = true
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    let appDelegate = UIApplication.shared.delegate as? AppDelegate
    appDelegate?.enableAllOrientation = false
}

关于ios - 只在某些特定的 ViewController 中启用横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56334313/

相关文章:

ios - 如果您不发布到 App Store,则在后台运行 iOS 应用程序

ios - 安装 cocoapods : Error with json-1. 8.3 及以上时出错

swift - 如何计算 Swift 中当前日期的剩余小时数

ios - 如何同步单元格动画

ios - 在 Realm 中存储具体的泛型子类

iOS Safari 问题 - 将绝对位置更改为固定时,元素在滚动时变得不可见

ios - Swift:将视频从 NSURL 保存到用户相机胶卷

swift - 如何使用 NSTimer 从 10 秒开始倒计时然后停止计时器?

ios - 我如何使用 AlamoFireObjectMapper 动态映射 JSON 中的值,该值取决于同一 JSON 中的另一个值?

swift - 在 Swift 中使用 FMDB 动态更新可变列数