4

All,
When i locked the screen orientation,i want use [[UIDevice currentDevice] orientation] to get screen's orientation ,but it always return UIDeviceOrientationPortrait, so, question is how can i get the real Device's orientation.
thank you very much.

Nitish
  • 13,845
  • 28
  • 135
  • 263
Yue Zhang
  • 201
  • 4
  • 14

4 Answers4

1

Make sure you're enabling orientation notifications via:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

If that still doesn't work, you can also use the status bar to figure out the current orientation (if your UI is rotating) via:

[[UIApplication sharedApplication] statusBarOrientation];

thomashw
  • 956
  • 4
  • 7
0

Another solution could be detect orientation from CoreMotion library. It would fix a bug when Portrait Orientation Lock: On. Refer that article: http://codrspace.com/yuvirajsinh/device-orientation-detection-using-coremotion/

0

If you're testing in the Simulator then be aware that the Simulator is terrible at returning accurate orientation values. Test on a device.

Alan Zeino
  • 4,406
  • 2
  • 23
  • 30
0

You need to turn on orientation notification generation first, as per the documentation.

You need to call:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

Then you can check the device's orientation. You need to tell the device that you are done getting the orientation, to conserve battery. You do so by calling:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

Personally, I've had better luck with using the status bar's orientation. This is because sometimes, (when the device is flat on a surface, for example) the device orientation won't be "Landscape" or "Portrait".

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • [[UIDevice currentDevice] orientation] does not work if the user has locked his device orientation from the Springboard. orientation is always the last value, and there are no notifications for changes. – Jeff Jul 16 '11 at 01:36