【问题标题】:Wrong View appearing when iPad screen is tipped back a certain angleiPad屏幕向后倾斜一定角度时出现错误视图
【发布时间】:2016-01-05 05:17:33
【问题描述】:

我的一个应用程序有一个问题,我在将应用程序安装到实际 iPad 上后才发现该问题,无法在模拟器中看到。问题是当我将 iPad 保持在横向时,如果我将 iPad 倾斜到某个角度,iPad 会保持横向模式,但视图会在仍处于横向模式时切换到我的 PortraitView。在我的代码中,我有一个名为 screenRotated() 的函数,它是 UIDeviceOrientationDidChangeNotification 的观察者。我的函数 screenRotated() 有以下代码:

let interfaceOrientation: UIDeviceOrientation = UIDevice.currentDevice().orientation
if UIDeviceOrientationIsLandscape(interfaceOrientation) {
    //does some stuff and then sets self.view = landscapeView
} else {
    //does some stuff and then sets self.view = portraitView
}

如何防止我的应用在横向时进入错误的视图?

【问题讨论】:

    标签: ios swift ipad uideviceorientation


    【解决方案1】:

    您的问题是您没有处理方向UIDeviceOrientationFaceUpUIDeviceOrietationFaceDown 的设备方向通知。这些既不是纵向也不是横向,当方向不是横向时,您的代码总是选择纵向。

    因此,当您向后倾斜时,它会朝上定向,并且您的代码会选择纵向,因为它不是横向而是朝上。

    因此,添加代码以检测面朝上/朝下,并在理想情况下保持上次设置的方向,直到您看到它实际上从纵向变为横向或相反。

    以下应该有效:

    if UIDeviceOrientationIsLandscape(interfaceOrientation) {
        //does some stuff and then sets self.view = landscapeView
    } else if UIDeviceOrientationIsPortrait(interfaceOrientation) {
        //does some stuff and then sets self.view = portraitView
    }
    else{
        // Do nothing
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 2014-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多