Programming Responses to iPhone Device Rotations

Responding to Device Rotations

One of the features that modern mobile devices support is the ability to detect the current orientation— portrait or landscape—of the device. An application can take advantage of this ability to readjust the device’s screen to maximize the use of the new orientation. A good example is Safari on the iPhone. When you rotate the device to landscape orientation, Safari automatically rotates its view so that you have a wider screen to view the content of the page (see Figure 6-1).


Figure 6-1

 In the iPhone SDK, there are several events that you can handle to ensure that your application is aware of changes in orientation. You can check it out in the following Try it Out.

Try It Out Supporting Different Screen Orientations

Codefile [] available for download at

1. Using Xcode, create a new View-Based Application project and name it ScreenRotations.

2. Press Command-R to test the application on the iPhone Simulator.

3. Change the iPhone Simulator orientation by pressing either the Command-? (rotate it to the right) or Command-? (rotate it to the left) key combination. Notice that the screen orientation of your application does not change with the change in device orientation (see Figure 6-2); the status bar is now vertical.

Figure 6-2

How It Works

By default, the iPhone application project you created using Xcode supports a single orientation—portrait mode. If you want to support screen orientations other than the default portrait mode, you can do so by overriding the shouldAutorotateToInterfaceOrientation: method in a view controller. This event is commented out by default in the ScreenRotationsViewController.m file:


// Override to allow orientations other than the default portrait orientation.

– (BOOL)shouldAutorotateToInterfaceOrientation:

    (UIInterfaceOrientation)interfaceOrientation {

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);



NOTE: One the iPhone, screen rotation is automatically handled by the OS. When the OS detects a change in screen orientation, it fires the shouldAutorotateToInterfaceOrientation: event; it is up to the developer to decide how the application should display in the target orientation.

The shouldAutorotateToInterfaceOrientation: method is called when the View is loaded and when orientation of the device changes. This event passes in a single parameter—the orientation that the device has been changed to. The returning value of this event determines whether the current orientation is supported.

If you want to support all screen orientations, simply return a YES:

– (BOOL)shouldAutorotateToInterfaceOrientation:

(UIInterfaceOrientation)interfaceOrientation {


    return YES; 


This means that your application will rotate to all orientations (the status bar will always appear at the top) when the device is rotated.

To support specific orientations, simply perform an equality check to specify the orientation supported. For example, the following code snippet shows that only the left landscape orientation is supported.


(UIInterfaceOrientation)interfaceOrientation {


    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft); 


This means that your application will display only in landscape mode (see Figure 6-3) with the Home button on its left (hence the constant name UIInterfaceOrientationLandscapeLeft).

Figure 6-3

If the user rotates the device to portrait mode or landscape mode with the Home button on the right (UIInterfaceOrientationLandscapeRight), the application will not change its orientation (see Figure 6-4).

Figure 6-4

NOTE: Here is one easy way to differentiate between UIInterfaceOrientationLandscapeLeft and UIInterfaceOrientationLandscapeRight. Just remember that UIInterfaceOrientationLandscapeLeft refers to the Home button positioned on the left and UIInterfaceOrientationLandscapeRight refers to the Home button positioned on the right.

This article is excerpted from chapter 6 "Screen Rotations" of the book Beginning iPhone SDK Programming with Objective-C by Wei-Meng Lee (ISBN: 978-0-470-50097-2, Wrox, 2010, Copyright Wiley Publishing Inc.)




Leave a Reply

Your email address will not be published. Required fields are marked *