Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDeviceOrientationFaceUp - how to distinguish between portrait and landscape?

Tags:

I'm trying to find out if the device is in portrait or landscape mode. My code works quite well if the device is not facing up. If it does face up (and orientation == 5), it won't distinguish between portrait and landscape. Is there anyway to determine the "orientation" in terms of landscape / portrait if the UIDeviceOrientation is FaceUp?

My code:

UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];

NSLog(@"orientation: %d", interfaceOrientation);

if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) {
    NSLog(@"LANDSCAPE!!!");
}

if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) {
    NSLog(@"PORTRAIT!!!");
}
like image 929
n.evermind Avatar asked Nov 05 '11 08:11

n.evermind


People also ask

What is difference between portrait and landscape?

The main difference between landscape and portrait orientation is how the camera is held when taking the photo. Landscape orientation is when the camera is held horizontally, while portrait orientation is when the camera is held vertically.

What does landscape orientation look like?

Landscape orientation generally refers to the camera's orientation in a horizontal display. This causes an image top and bottom edges to be longer than the sides. Likewise, landscape makes the image's elements to be broader than taller.

Is horizontal portrait or landscape?

Landscape refers to the orientation that is wider than it is tall. It's the horizontal option. Portrait, on the other hand, is taller than it is wide, which makes it the vertical option.

What is the difference between landscape?

The main difference between landscape and portrait image orientation is that a landscape image is wider than it is taller while a portrait image is taller than it is wider. In other words, Landscape images are captured in a horizontal layout while portrait images are captured in a vertical layout.


2 Answers

You should not confuse UIDeviceOrientation and UIInterfaceOrientation, they are different but related as shown by their declaration

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

typedef enum {
   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;

UIDeviceOrientation tells you what the orientation of the device is. UIInterfaceOrientation tells you what the orientation of your interface is, and is used by UIViewController. UIInterfaceOrientation will clearly be either portrait or landscape, whereas UIDeviceOrientation can have ambiguous values (UIDeviceOrientationFaceUp, UIDeviceOrientationFaceDown, UIDeviceOrientationUnknown).

In any case you should not attempt to determine the orientation of a UIViewController with [[UIDevice currentDevice] orientation], as no matter what the device orientation is the UIViewController interfaceOrientation property can be different (for example if your app does not rotate to landscape at all [[UIDevice currentDevice] orientation] can be UIDeviceOrientationLandscapeLeft while viewController.interfaceOrientation can be UIInterfaceOrientationPortrait).

Update: As of iOS 8.0, [UIViewController interfaceOrientation] is deprecated. An alternative offered here is [[UIApplication sharedApplication] statusBarOrientation]. This also returns UIInterfaceOrientation.

like image 161
jbat100 Avatar answered Oct 31 '22 18:10

jbat100


I make this code skeleton for dealing with wanted & unwanted devices orientations, in my case i want to ignore the UIDeviceOrientationUnknown, UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown, caching the last allowed orientation. This code deals with iPhone and iPad devices and can be useful for you.

- (void)modXibFromRotation {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    NSString *device = [[UIDevice currentDevice]localizedModel];
    UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];

    if ([device isEqualToString:@"iPad"]) {

        if (orientation == UIDeviceOrientationUnknown || 
            orientation == UIDeviceOrientationFaceUp || 
            orientation == UIDeviceOrientationFaceDown) {

                orientation = (UIDeviceOrientation)cachedOrientation;
        }

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

            /* Your code */
        }

        if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {

            /* Your code */     
        }
    }

    if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) {

        if (orientation == UIDeviceOrientationUnknown || 
        orientation == UIDeviceOrientationFaceUp || 
        orientation == UIDeviceOrientationFaceDown) {

            orientation = (UIDeviceOrientation)cachedOrientation;
        }

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

             /* Your code */
        }

        if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {

            /* Your code */
        }
    }
}
like image 22
MarioGT Avatar answered Oct 31 '22 16:10

MarioGT