In my iPad app, I need to run some layout code to set the proper layout depending on the orientation. By default, the layout is configured for the landscape orientation, so
Not that you need another answer, but I thought I should add that you almost never want to use [[UIDevice currentDevice] orientation]
. That method returns the orientation of the device, which isn't necessarily the same as the orientation of the interface.
Use self.interfaceOrientation
in your view controller - it's a property of UIViewController
that is set by iOS for you, and in some cases is more reliable than [[UIDevice currentDevice] orientation]
.
Here's a detailed description: http://bynomial.com/blog/?p=25
As mentioned in a blog post above, there is a set of macros for testing orientation. That blog post however mentions UIDeviceOrientationIsPortrait. I like the following below, it's a minor twist.
if(UIInterfaceOrientationIsPortrait(self.interfaceOrientation))
{
NSLog(@"Portrait");
}
else
{
NSLog(@"Landscape");
}
An observation I've made is that you can't call this code in a table view, pushed on to a Navigation Controller embedded in the split view controller. So in other words you can't call it from the master view controller. You have to replace the "self.interfaceOrientation" with splitviewcontroller.interfaceOrientation, assuming you maintain a reference to the parent split view controller.