when iphone Device orientation is face up/down, can I tell if it´s landscape or portrait?
Asked Answered
D

4

20

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;
    }
}
Dita answered 11/9, 2013 at 13:33 Comment(0)
S
10

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.

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.

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

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

Sari answered 11/9, 2013 at 13:42 Comment(3)
@frédéric-adda has a Swift 3 / iOS 11 answer down below. https://mcmap.net/q/619139/-when-iphone-device-orientation-is-face-up-down-can-i-tell-if-it-180-s-landscape-or-portraitChris
that is weird, iOS 12 is here and this issue still isnt resolved. Also, they have deprecated the one which is working properly. :/Aryl
This is deprecatedHuertas
T
16

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

UIDeviceOrientationIsPortrait(orientation)  
UIDeviceOrientationIsLandscape(orientation)  

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

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.

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

Thatcher answered 20/9, 2017 at 12:57 Comment(3)
In some places this is helpful. In others you need to check the size of the outer VC and compare it to the Window. One issue with this, however: you also need to return true if isLandscape is true. Thanks!Layby
UIApplication.shared.statusBarOrientation is also deprecated nowKanazawa
I changed my method to check the orientation.Thatcher
S
10

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.

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.

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

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

Sari answered 11/9, 2013 at 13:42 Comment(3)
@frédéric-adda has a Swift 3 / iOS 11 answer down below. https://mcmap.net/q/619139/-when-iphone-device-orientation-is-face-up-down-can-i-tell-if-it-180-s-landscape-or-portraitChris
that is weird, iOS 12 is here and this issue still isnt resolved. Also, they have deprecated the one which is working properly. :/Aryl
This is deprecatedHuertas
A
3

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

 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.

Azotic answered 11/9, 2013 at 13:42 Comment(1)
this does not work UIDeviceOrientationIsLandscape(orientation) i am passing UIDeviceOrientationIsLandscape(.faceup) it always give falseRademacher
A
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.

Aptitude answered 12/12, 2015 at 9:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.