views:

4946

answers:

3
+3  Q: 

iPhone orientation

The possible values for UIDevice.orientation include UIDeviceOrientationFaceUp and UIDeviceOrientationFaceDown. While it might be useful to know that the device is flat, this doesn't tell us whether it's flat displaying an interface oriented in portrait or landscape mode. Is there a way to find the current orientation of the GUI in cases where the device is returning ambiguous information? I suppose I could track orientation change events to remember the last portrait/landscape orientation or check the main UIView's bounds height and width, but that seems klugey. Is there a property on the device or UIView that I'm missing?

+14  A: 

In a viewcontroller you can simply use the code:

UIInterfaceOrientation interfaceOrientation = self.interfaceOrientation;

The UIInterfaceOrientation is an enum:

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

I've seen the same situation. Specifically in this flow:

  • Controller 1 loads in say.. Portrait.
  • Push another controller onto the stack
  • While in the 2nd controller, rotate the device orientation to Landscape.
  • Lay the device flat on a table and then pop back to the original controller.

At this point, any orientation checks I've seen return an invalid orientation of 5, so it's not directly possible to determine if a landscape or portrait layout should be used. (I'm doing custom layout positioning on a per-orientation basis, so this is significant information)

In my case, a bounds width check on the view is used to determine the true state of things, but I'd love to know if others have addressed differently.

jberry
+2  A: 

Also you can get current orientation in any point (not only ViewController) of you code like this:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
beryllium
it is not UIInterfaceOrientation, it is - UIDeviceOrientation, that's what micco already mentioned in his post.
justadreamer