Changing Views to Landscape Mode

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.

  1. 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

Comments are closed.