Get current orientation of iPad?

MikeN picture MikeN · Apr 29, 2010 · Viewed 50.5k times · Source

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.

Answer

Paul Lynch picture Paul Lynch · Apr 29, 2010

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