【问题标题】:when iphone Device orientation is face up/down, can I tell if it´s landscape or portrait?当 iphone 设备方向朝上/朝下时,我可以判断它是横向还是纵向?
【发布时间】:2013-09-11 13:33:26
【问题描述】:

我得到这段代码,如果设备处于横向左/右或倒置状态,它会旋转并显示另一个视图控制器。但是如果它的方向是朝上还是朝下,那么我怎么知道它是横向模式还是纵向模式?因为我只想在它朝上或朝下并处于横向模式时旋转

    - (void)viewDidAppear:(BOOL)animated
    {
        UIDeviceOrientation orientation = [[UIDevice currentDevice]orientation];
        NSLog(@"orientation %d", orientation);
        if ((orientation == 2) || (orientation == 3) || (orientation == 4))
        {

            [self performSegueWithIdentifier:@"DisplayLandscapeView" sender:self];
            isShowingLandscapeView = YES;
    }
}

【问题讨论】:

    标签: ios uiviewcontroller uideviceorientation


    【解决方案1】:

    interfaceOrientation 属性自 iOS 8 起已弃用。 以及辅助方法

    UIDeviceOrientationIsPortrait(orientation)  
    UIDeviceOrientationIsLandscape(orientation)  
    

    也无济于事,因为当方向为 .faceUp 时它们返回 false。

    所以我以这种方式结束了检查:

    extension UIViewController {
        var isPortrait: Bool {
            let orientation = UIDevice.current.orientation
            switch orientation {
            case .portrait, .portraitUpsideDown:
                return true
            case .landscapeLeft, .landscapeRight:
                return false
            default: // unknown or faceUp or faceDown
                guard let window = self.view.window else { return false }
                return window.frame.size.width < window.frame.size.height
            }
        }
    }
    

    这是在 UIViewController 扩展中,所以如果其他一切都失败了,我可以恢复比较屏幕宽度和高度。

    我使用window 是因为如果当前 ViewController 嵌入在容器中,它可能无法反映全局 iPad 方向。

    【讨论】:

    • 在某些地方这很有帮助。在其他情况下,您需要检查外部 VC 的大小并将其与 Window 进行比较。然而,一个问题是:如果isLandscape 为真,您还需要返回真。谢谢!
    • UIApplication.shared.statusBarOrientation 现在也已弃用
    • 我改变了检查方向的方法。
    【解决方案2】:

    在 UI 代码中,您通常不应该依赖于设备方向,而是用户界面方向。它们之间通常存在差异,例如当视图控制器仅支持纵向时。

    您的情况最重要的区别是界面方向永远不会朝上/朝下。

    在您的情况下,您只需向视图控制器询问当前的用户界面方向:self.interfaceOrientation

    您的条件可以表达为有点像if (deviceOrientation is face up/down and interfaceOrientation is landscape)

    请记住,向左的设备方向横向意味着向右的用户界面方向。

    【讨论】:

    • @frédéric-adda 在下面有一个 Swift 3 / iOS 11 的答案。 stackoverflow.com/a/46322727/1758224
    • 这很奇怪,iOS 12 来了,这个问题仍然没有解决。此外,他们已经弃用了正常工作的那个。 ://
    • 已弃用
    【解决方案3】:

    是的,您可以,UIDeviceOrientation 是一个枚举,其中包含:

     UIDeviceOrientationUnknown,
     UIDeviceOrientationPortrait,          
     UIDeviceOrientationPortraitUpsideDown,
     UIDeviceOrientationLandscapeLeft,     
     UIDeviceOrientationLandscapeRight,    
     UIDeviceOrientationFaceUp,            
     UIDeviceOrientationFaceDown    
    

    甚至还有两个助手:

    UIDeviceOrientationIsPortrait(orientation)  
    UIDeviceOrientationIsLandscape(orientation) 
    

    只需在UIDeviceOrientation 上执行 cmd 即可显示声明枚举的头文件。

    【讨论】:

    • 这不起作用 UIDeviceOrientationIsLandscape(orientation) 我正在传递 UIDeviceOrientationIsLandscape(.faceup) 它总是给出错误
    【解决方案4】:

    有一种方法可以检查它。

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    UIInterfaceOrientation statusBarOrientation =[UIApplication sharedApplication].statusBarOrientation;
    

    使用第一个检查设备是否正面朝上,第二个会告诉您设备是纵向还是横向。

    【讨论】:

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