iOS: Device orientation on load

EDIT: I mis-read your question. This will allow you to start your application in certain orientations. Just realized you’re trying to figure out the orientation on launch.

There is a method to check the status bar orientation on UIApplication:

[[UIApplication sharedApplication] statusBarOrientation];

Original answer

Try setting the application’s accepted device orientations in the plist file:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

This will indicate that your application supports Portrait (home button at the bottom), landscape left, and landscape right.

Then, in your UIViewControllers, you will need to override the shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) method to return YES when the app should rotate:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

     return interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

This will tell the UIViewController to auto rotate if the device is in one of your supported orientations. If you wanted to support the upside-down orientation as well (portrait with home button on top) then add that to your plist and just return YES out of this method.

Let us know how it works out.

Leave a Comment