开发者

UIOrientation returns 0 or 5

开发者 https://www.devze.com 2023-01-09 05:57 出处:网络
I am running a simple function that get\'s called in multiple areas to help deal with layout on an iPad app during orientation changes. It looks like this:

I am running a simple function that get's called in multiple areas to help deal with layout on an iPad app during orientation changes. It looks like this:

- (void) getWidthAndHeightForOrientation:(UIInterfaceOrientation)orientation {
    NSLog(@"New Orientation: %d",orientation);
end

And I call it in v开发者_如何学运维arious places like this:

[self getWidthAndHeightForOrientation: [[UIDevice currentDevice] orientation]];

The function normally has some simple code that runs if the orientation is portrait or landscape. Unfortunately it wasn't working as expected when the app is started in what would be position 1. I get 0 as a result. Later if the function is called in the same manner but the device has never been rotated I get back a value of 5. What does this mean? Why would it throw these values?

In short why would [[UIDevice currentDevice] orientation] ever throw 0 or 5 instead of any value between 1 and 4?

UPDATE:

Because I kept finding bugs in my code due to the way orientation was handled, I wrote a definitive post on how to handle UIDevice or UIInterface orientations: http://www.donttrustthisguy.com/orientating-yourself-in-ios


Did you take a look at the enum values for UIInterfaceOrientation? From the docs:

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

So it could conceivably be anything from 0-6.

Edit: Maybe you should be using the methods on your UIViewController (willRotateToInterfaceOrientation:duration:, etc.) instead of calling orientation on the UIDevice?


I would recommend using UIDeviceOrientationIsValidInterfaceOrientation(orientation)

It will tell you if its a valid orientation (valid being either landscape or portrait, not FaceUp/FaceDown/UnKnown). Then you can treat it as if its portrait if its unknown.

This is how I do it:

if (UIDeviceOrientationIsValidInterfaceOrientation(interfaceOrientation) && UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
    // handle landscape
} else {
    // handle portrait
}


[UIDevice currentDevice].orientation returns a UIDeviceOrientation:

The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application’s user interface.

Your getWidthAndHeightForOrientation function takes a UIInterfaceOrientation parameter:

The orientation of the application's user interface.

These types, though related, are not the same thing. You can access the current interface orientation from any view controller using self.interfaceOrientation. UIInterfaceOrientation has 4 possible values while UIDeviceOrientation has 9:

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
};

// Note that UIInterfaceOrientationLandscapeLeft is equal to UIDeviceOrientationLandscapeRight (and vice versa).
// This is because rotating the device to the left requires rotating the content to the right.
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
};
0

精彩评论

暂无评论...
验证码 换一张
取 消