How to get the iPhone view's current orientation or the device's last orientation?
Asked Answered
H

2

9

In my app an image appears when user shakes the device. There are different images for landscape and portrait mode. If the iPhone's orientation is portrait or landscape, it works fine. But if it is UIDeviceOrientationFaceDown or UIDeviceOrientationFaceUp, the image should depend on the last orientation used, because the view is still displayed for this orientation.

How can I get the last orientation of the device?

Hughmanick answered 19/8, 2010 at 14:56 Comment(0)
H
26

I've figured it out! I had to check the status bar's orientation!

[UIApplication sharedApplication].statusBarOrientation
Hughmanick answered 19/8, 2010 at 15:52 Comment(1)
What if the status bar is hidden ?Turd
S
2

Well in the shouldAutoRotate method of a class it passes in a parameter called interfaceOrientation. This method is called anytime the device orientation changes. You can store this as a variable and the look at it at a later time to determine the last orientation.

 1 = standard portrait
 2 = upsidedown
 3 = button right landscape
 4 = button left landscape. 

Also the call of self.interfaceOrientation will give you the current device orientation.

Hope this at least get you started in the correct direction.

Sanguinolent answered 19/8, 2010 at 15:8 Comment(1)
self.interfaceOrientation gives me just UIDeviceOrientationFaceDown (Up), so it doesn't help. I'll try to find out if the interfaceOrientation of the shouldAutoRotate method is overwritten when the orientation is FaceUp. Thank you!Hughmanick

© 2022 - 2024 — McMap. All rights reserved.