【问题标题】:Lock one view orientation in iOS在 iOS 中锁定一个视图方向
【发布时间】:2016-05-23 17:35:29
【问题描述】:

我正在尝试以纵向模式锁定我的应用的一个视图。我有一个顶部导航控制器视图和多个嵌套场景。

我找到了很多关于锁定方向问题的答案并尝试了下面的代码,但它只能部分工作。让场景 A 和 B,其中 A 有一个segue 到 B,而 B 被锁定在纵向。如果我去 B 场景,将设备转到横向位置并返回 A,方向是从之前的 B(纵向)继承的。

如何让 A 场景识别实际的设备方向?

导航控制器

internal override func shouldAutorotate() -> Bool {
    return visibleViewController!.shouldAutorotate()
}

“B”视图控制器

override func shouldAutorotate() -> Bool {
    if UIDevice.currentDevice().orientation == .Portrait {
        return true
    }
    return false
}

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

override func viewDidLoad() {
    super.viewDidLoad()
    let orientation = UIInterfaceOrientation.Portrait.rawValue
    UIDevice.currentDevice().setValue(orientation, forKey: "orientation")
}

【问题讨论】:

  • 查看我的答案,如果对您有用,请给我反馈

标签: ios swift uiinterfaceorientation uideviceorientation


【解决方案1】:

据我了解,您想知道如何获取当前设备方向。

试试这个...

var orientation = UIDevice.currentDevice().orientation

【讨论】:

  • 它不起作用。一旦我在viewDidLoad 中设置了方向,我就失去了这个功能。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
相关资源
最近更新 更多