views:

143

answers:

1

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 various 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?

+1  A: 

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?

Wevah
Once I change the orientation by rotating the device it is always proper. But I don't understand why it would be Unknown or return FaceUp. Is there a function I need to call to have it find the orientation if it is currently unknown?
Jim Jeffers
I assume that FaceUp is returned when the device is parallel to the ground and hasn't been rotated yet. (I edited my answer a little because apparently I can't count.)
Wevah
I've edited my answer again after looking some stuff up. (I admit I mostly do desktop stuff.)
Wevah
Thanks - I believe you are right. I've updated my code to assume the application is in portrait if unknown has been returned. This seems to have solved my dilemma.
Jim Jeffers