View Rotation using Transforms
The default view coordinate system assumes a portrait orientation. To launch in landscape, you have to change the orientation of your view, and the way you do this is by modifying the view's affine transform. Translating a view shifts all its subviews.
After your views are loaded into memory, you need to reorient them to landscape mode by doing the following:Find the center point of your application’s content area.
Rotate your views 90 degrees around that center point.
The exact center point of your application’s content area can vary and is dependent on your interface design. Applications that do not display the status bar or applications that display a translucent status bar typically have a content area that matches the screen bounds. Applications that display an opaque status bar typically have a content area that does not include the area occupied by the status bar. This latter scenario is more common and is also a little more challenging to implement correctly. As a result, the rest of this Tech Note shows you how to perform a rotation on an interface that uses an opaque status bar.
When your landscape view is loaded from its nib file, its center point is initially set to match the center point of the screen, as shown in the Figure below: “Coordinates used when laying out content in landscape mode”. Because the view in the example does not occupy the area under the status bar, however, the initial center point is incorrect. To find the correct center point, you can use the location and size of the status bar to aid your computations. The status bar’s height represents the width of the landscape-oriented content area. Similarly, the status bar’s x-origin represents the height of the landscape-oriented content area. Using these values along with the origin point of (0, 0), you can compute the correct center point of the content area and use that value to reposition your view before applying a rotation transform.Figure 1 Coordinates used when laying out content in landscape mode.
The Listing below "Reorienting a view to landscape mode” shows the viewDidLoad: method of a controller object whose views are oriented for landscape mode at launch. As previously described, this method uses the height and x-origin of the status bar to compute the center point of the landscape-oriented content area. It then shifts the center of the portrait-oriented view to that location and rotates the view into the proper orientation.
Listing 2 Reorienting a view to landscape mode.
- (void)viewDidLoad
{
[super viewDidLoad];
if ([[UIDevice currentDevice].systemVersion floatValue] < 2.1)
{
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationLandscapeRight)
{
CGAffineTransform transform = self.view.transform;
// use the status bar frame to determine the center point of the window's content area.
CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
CGPoint center = CGPointMake(bounds.size.height / 2.0, bounds.size.width / 2.0);
// set the center point of the view to the center point of the window's content area.
self.view.center = center;
// Rotate the view 90 degrees around its new center point.
transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
self.view.transform = transform;
}
}
}