Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically determine iPhone interface 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 kludgey. Is there a property on the device or UIView that I'm missing?

like image 619
micco Avatar asked Mar 11 '09 14:03

micco


People also ask

How do you check orientation on iPhone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.

How can we detect the orientation of the screen?

You can detect this change in orientation on Android as well as iOS with the following code: var supportsOrientationChange = "onorientationchange" in window, orientationEvent = supportsOrientationChange ?

How do I get device orientation in Swift?

The value of the property is a constant that indicates the current orientation of the device. This value represents the physical orientation of the device and may be different from the current orientation of your application's user interface. See “UIDeviceOrientation” for descriptions of the possible values. UIDevice.

What is Interface orientation?

Interface Orientation can be anything, regardless of device orientation. Device Orientation is the actual physical orientation of the device you are holding, and this is not variable; it is what it is. If you are holding in Portrait, it is Portrait.


4 Answers

You can get orientation by 3 ways:

  1. UIInterfaceOrientation orientation = self.interfaceOrientation; returns UIInterfaceOrientation, current orientation of the interface. It is a property in UIViewController, you can access to this one only in UIViewController classes.

  2. UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; returns UIInterfaceOrientation, current orientation of the application's status bar. You can access to that property in any point of your application. My experience shows that this is the most effective way to retrieve real interface orientation.

  3. UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; returns UIDeviceOrientation, device orientation. You can access to that property in any point of your application. But note that UIDeviceOrientation is not always UIInterfaceOrientation. For example, when your device is on a plain table you can receive unexpected value.

like image 141
beryllium Avatar answered Oct 10 '22 07:10

beryllium


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;
like image 32
georg Avatar answered Oct 10 '22 07:10

georg


If you just care that the device is landscape or portrait, there's some nice convenience methods on the viewcontroller:

UIDeviceOrientationIsLandscape(self.interfaceOrientation)
UIDeviceOrientationIsPortrait(self.interfaceOrientation)
like image 14
Chris O'Sullivan Avatar answered Oct 10 '22 07:10

Chris O'Sullivan


Status bar orientation (statusBarOrientation) always returns the interface orientation even if the status bar is hidden.

You can use the status bar orientation without a view controller. It gives you the current view orientation, not the device orientation.

UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
like image 11
Tibidabo Avatar answered Oct 10 '22 08:10

Tibidabo