Please consider subscribing to Mobile Developer Tips RSS feed or following us on Twitter
|
|
|
As you well know, by default, iPhone applications launch in portrait mode. If you need to start your application in landscape mode, you can set UIInterfaceOrientation key in the plist file. The two possible values for this key are: UIInterfaceOrientationLandscapeLeft (iPhone home button will be on the left) and UIInterfaceOrientationLandscapeRight (home button on right).
For views to be shown in landscape mode, you must manually rotate the coordinate system 90 degrees. Here’s one way you can do that, let’s assume we have a class that is of type UIViewController:
1
2
3
4
5
| @interface SomeViewController : UIViewController
{
...
}
@end |
Inside the implementation file of SomeViewController, here’s one approach you can use within the viewDidLoad method:
1
2
3
4
5
6
| - (void)viewDidLoad
{
self.view.transform = CGAffineTransformIdentity;
self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
self.view.bounds = CGRectMake(0.0, 0.0, 480, 320);
} |
Specifying CGAffineTransformMakeRotation call with 90 degrees as shown above will show the view in the UIInterfaceOrientationLandscapeRight. Setting this value to -90 will show the view in the UIInterfaceOrientationLandscapeLeft.
If you set the orientation in the plist file, you’ll need to coordinate the value set in the loadView method so the status bar appears as expected, that is, across the top of the display. If you choose set the orientation in the plist as UIInterfaceOrientationLandscapeLeft, use -90 as the rotation value, set to 90 for UIInterfaceOrientationLandscapeRight.
There are more ways than one to rotate views to landscape mode, if you have a preferred means, please post a comment.
Related posts:
- Changing Application Name on Home Screen
Comments
2 Responses to “Changing Views to Landscape Mode”
Leave Comment
i have read several posts and you always are to the point yet easy to understand. point you out to friends often. and yes this landscape code works great
[Reply]
thank you for this code, I’ve been grappling with this as you can see here: http://stackoverflow.com/questions/738216/iphone-landscape-only-app-view-axis-confusion
So I see the key difference between what I had done and what you had done, is you went and set the bounds explicitly. Why? Why doesn’t the rotation actually do that for me, since I create the view with the application frame and then I rotate it?
[Reply]