当iphone设备方向朝上/朝下时,我可以判断它是风景还是肖像?

时间:2022-07-03 21:09:38

I got this code that if the device is in landscape left/right or upside down it rotates and shows another view controller. but if it´s in the orientation face up or face down, then how can I tell if it´s in landscape mode or portrait? cause I only want to rotate if it´s face up or down and in landscape mode

我得到了这个代码,如果设备是横向左/右或倒置它旋转并显示另一个视图控制器。但如果它朝上或面朝下,那么如何判断它是横向模式还是纵向?因为我只想面朝上或朝下并在横向模式下旋转

    - (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;
    }
}

4 个解决方案

#1


6  

In UI code you usually should not depend on the device orientation but the user interface orientation. There's often a difference between them, for example when a view controller only supports portrait.

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

The most important difference for your case is that the interface orientation is never face up/down.

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

In your case you can just ask the view controller for the current user interface orientation: self.interfaceOrientation.

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

Your condition could be expressed somewhat like if (deviceOrientation is face up/down and interfaceOrientation is landscape)

你的情况可能有点像if(deviceOrientation面向上/向下,interfaceOrientation是横向)

Bear in mind that a device orientation landscape left means a user interface orientation landscape right.

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

#2


10  

The interfaceOrientation property is deprecated since iOS 8. And the helpers methods

从iOS 8开始,不推荐使用interfaceOrientation属性。还有helpers方法

UIDeviceOrientationIsPortrait(orientation)  
UIDeviceOrientationIsLandscape(orientation)  

won't help either, because they return false when orientation is .faceUp.

也没有帮助,因为当方向是.faceUp时它们会返回false。

So I ended checking this way:

所以我结束了检查:

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
        }
    }
}

This is in a UIViewController extension, so I can revert to comparing the screen width and height if everything else fails.

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

I use window because if the current ViewController is embedded in a container, it may not reflect the global iPad orientation.

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

#3


3  

Yes you can, the UIDeviceOrientation is an enum which contains:

是的,你可以,UIDeviceOrientation是一个包含以下内容的枚举:

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

There are even two helpers:

甚至还有两个助手:

UIDeviceOrientationIsPortrait(orientation)  
UIDeviceOrientationIsLandscape(orientation) 

Just cmd on UIDeviceOrientation to show the headerfile where the enum is declared.

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

#4


3  

There is one way to check it.

有一种方法可以检查它。

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

Use the first one to check if the device is in faceup and second one will tell you if the device is in portrait or landscape.

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

#1


6  

In UI code you usually should not depend on the device orientation but the user interface orientation. There's often a difference between them, for example when a view controller only supports portrait.

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

The most important difference for your case is that the interface orientation is never face up/down.

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

In your case you can just ask the view controller for the current user interface orientation: self.interfaceOrientation.

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

Your condition could be expressed somewhat like if (deviceOrientation is face up/down and interfaceOrientation is landscape)

你的情况可能有点像if(deviceOrientation面向上/向下,interfaceOrientation是横向)

Bear in mind that a device orientation landscape left means a user interface orientation landscape right.

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

#2


10  

The interfaceOrientation property is deprecated since iOS 8. And the helpers methods

从iOS 8开始,不推荐使用interfaceOrientation属性。还有helpers方法

UIDeviceOrientationIsPortrait(orientation)  
UIDeviceOrientationIsLandscape(orientation)  

won't help either, because they return false when orientation is .faceUp.

也没有帮助,因为当方向是.faceUp时它们会返回false。

So I ended checking this way:

所以我结束了检查:

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
        }
    }
}

This is in a UIViewController extension, so I can revert to comparing the screen width and height if everything else fails.

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

I use window because if the current ViewController is embedded in a container, it may not reflect the global iPad orientation.

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

#3


3  

Yes you can, the UIDeviceOrientation is an enum which contains:

是的,你可以,UIDeviceOrientation是一个包含以下内容的枚举:

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

There are even two helpers:

甚至还有两个助手:

UIDeviceOrientationIsPortrait(orientation)  
UIDeviceOrientationIsLandscape(orientation) 

Just cmd on UIDeviceOrientation to show the headerfile where the enum is declared.

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

#4


3  

There is one way to check it.

有一种方法可以检查它。

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

Use the first one to check if the device is in faceup and second one will tell you if the device is in portrait or landscape.

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