RESPONDING TO DEVICE ROTATIONS

One of the features that modern mobile devices support is the capability to detect the current orientation — portrait or landscape — of the device. An application can take advantage of this to re-adjust the device's screen to maximize 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 7-1).

The iOS SDK contains several events that you can handle to ensure that your application is aware of changes in orientation. Check them out in the following Try It Out.

image

FIGURE 7-1

TRY IT OUT: Supporting Different Screen Orientations

image

  1. Using Xcode, create a new Single View Application (iPhone) project and name it ScreenRotations. You will also use the project name as the Class Prefix and ensure that you have the Use Automatic Reference Counting option unchecked.
  2. Press Command-R to test the application on the iPhone 4 Simulator.
  3. Change the iPhone Simulator orientation by pressing either Command-→ (rotate it to the right) or Command-← (rotate it to the left) key combination. Observe that the application stays upright when the Simulator is either in portrait (upright) mode or in landscape ...

Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.