UIOrientation returns 0 or 5

I run a simple function that is called in several areas to help deal with the layout in the iPad application during orientation changes. It looks like this:

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

And I call it in different places:

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

A function usually has some simple code that executes if the orientation is portrait or landscape. Unfortunately, it did not work as expected, when the application starts in what will be at position 1. As a result, I get 0. Later, if the function is called in the same way, but the device never rotates, I return 5. What it means? Why is it throwing these values?

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

UPDATE:

As I continued to find errors in my code due to how orientation was handled, I wrote the final post on how to handle UIDevice or UIInterface orientations: http://www.donttrustthisguy.com/orientating-yourself-in-ios

+4
source share
3 answers

I would recommend using UIDeviceOrientationIsValidInterfaceOrientation(orientation)

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

Here is how I do it:

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

Have you looked at the enumeration values ​​for UIInterfaceOrientation ? From the docs:

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

So it can be anything from 0-6.

Edit: Perhaps you should use the methods on the UIViewController ( willRotateToInterfaceOrientation:duration: etc.) instead of calling orientation on the UIDevice ?

+8
source

[UIDevice currentDevice].orientation returns a UIDeviceOrientation :

The property value is a constant indicating the current orientation of the device. This value represents the physical orientation of the device and may differ from the current orientation of your application user interface.

Your getWidthAndHeightForOrientation function accepts the UIInterfaceOrientation parameter:

Orientation of the user interface of the application.

These types, although related to each other, are not the same thing. You can access the current interface orientation from any view controller using self.interfaceOrientation . UIInterfaceOrientation has 4 possible values, and 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
source

All Articles