Orientation Changes

The iPhone is retrofitted with hardware to sense its state in the surrounding environment. One sensor in particular, the accelerometer, is able to determine the orientation that the iPhone is being held at. How to read the orientation and what to do with it when it's changed are important for applications that need to provide landscape mode support.

Reading the Orientation

The orientation of the iPhone can be read using a static method named deviceOrientation, found in the UIHardware class:

int orientation = [ UIHardware deviceOrientation: YES ];

This method returns one of six different possible orientations identifying how the iPhone is presently being held.

Orientation

Description

0

kOrientationFlatUp: Device is laying flat, as if face up on a surface

1

kOrientationVertical: Device is held vertically, rightside-up

2

kOrientationVerticalUpsideDown: Device is held vertically, upside-down

3

kOrientationHorizontalLeft: Device is tipped to the left on its side

4

kOrientationHorizontalRight: Device is tipped to the right on its side

5

kOrientationUnknown: Device state unknown; sensor failure?

6

kOrientationFlatDown: Device is laying flat, as if face down on a surface

The sensor can be read when the application first starts up, but what's more useful is to know when the orientation has been changed. A change in the orientation is reported automatically to the UIApplication class, the class your GUI application is derived from. A method named deviceOrientationChanged can be overridden ...

Get iPhone Open 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.