Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get current orientation of iPad?

In a given event handler (not the "shouldAutorotateToInterfaceOrientation" method) how do I detect the current iPad orientation? I have a text field I have to animate up (when keyboard appears) in the Landscape view, but not in the portrait view and want to know which orientation I'm in to see if the animation is necessary.

like image 520
MikeN Avatar asked Apr 29 '10 15:04

MikeN


People also ask

How do I get the current orientation in Objective C?

The easiest way to get to this, is to use the property interfaceOrientation on your UIViewController. (So most often, just: self. interfaceOrientation will do). Remember: Left orientation is entered by turning your device to the right.

What is device orientation in Xcode?

Upside Down: This orientation means you change the orientation of your iOS device 180 degrees. But if you check this checkbox only, you will get the below error message when you run the app. So this checkbox should be used with other orientations such as Portrait or Landscape.


2 Answers

Orientation information isn't very consistent, and there are several approaches. If in a view controller, you can use the interfaceOrientation property. From other places you can call:

[[UIDevice currentDevice] orientation] 

Alternatively, you can request to receive orientation change notifications:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

Some people also like to check the status bar orientation:

[UIApplication sharedApplication].statusBarOrientation 
like image 112
Paul Lynch Avatar answered Oct 02 '22 07:10

Paul Lynch


I think

[[UIDevice currentDevice] orientation]; 

is not really reliable. Sometimes it works, sometimes not... In my apps, I use

[[UIApplication sharedApplication]statusBarOrientation];  

and it works great!

like image 41
Samuel Avatar answered Oct 02 '22 07:10

Samuel