Home > Articles > Mobile Application Development & Programming

This chapter is from the book

Working with Basic Orientation

The UIDevice class uses the built-in orientation property to retrieve the physical orientation of the device. iOS devices support seven possible values for this property:

  • UIDeviceOrientationUnknown—The orientation is currently unknown.
  • UIDeviceOrientationPortrait—The home button is down.
  • UIDeviceOrientationPortraitUpsideDown—The home button is up.
  • UIDeviceOrientationLandscapeLeft—The home button is to the right.
  • UIDeviceOrientationLandscapeRight—The home button is to the left.
  • UIDeviceOrientationFaceUp—The screen is face up.
  • UIDeviceOrientationFaceDown—The screen is face down.

The device can pass through any or all of these orientations during a typical application session. Although orientation is created in concert with the onboard accelerometer, these orientations are not tied in any way to a built-in angular value.

iOS offers two built-in macros to help determine if a device orientation enumerated value is portrait or landscape: namely UIDeviceOrientationIsPortrait() and UIDeviceOrientationIsLandscape(). It is convenient to extend the UIDevice class to offer these tests as built-in device properties:

@property (nonatomic, readonly) BOOL isLandscape;
@property (nonatomic, readonly) BOOL isPortrait;

- (BOOL) isLandscape
{
    return UIDeviceOrientationIsLandscape(self.orientation);
}

- (BOOL) isPortrait
{
    return UIDeviceOrientationIsPortrait(self.orientation);
}

Your code can subscribe directly to device reorientation notifications. To accomplish this, send beginGeneratingDeviceOrientationNotifications to the currentDevice singleton. Then add an observer to catch the ensuing UIDeviceOrientationDidChangeNotification updates. As you would expect, you can finish listening by calling endGeneratingDeviceOrientationNotification.

  • + Share This
  • 🔖 Save To Your Account