iPhone Development 101

iPhone 101

Code Tips

Resources

Links

Follow

Mailing ListTwitter

More

iPhone Development 101: User Interface:
Detecting iPhone Device Sizes

Screen Dimensions

To get the screen dimensions (in points) of the current device:

CGRect screenBounds = [[UIScreen mainScreen] bounds];

On the iPhone 5, the screen height is 568. On earlier iPhone models, it's 480. On the iPad, it's 1024.

You can use the following macro to detect if it's a 4" (568-pixel) screen (add below the #import directives, or add to your app's main Appname-Prefix.pch file to make it globally accessible):

#define IS_WIDESCREEN ( [ [ UIScreen mainScreen ] bounds ].size.height == 568 )

Then in the viewDidLoad method, use the macro to do any special setup needed for the iPhone 5 screen:

if (IS_WIDESCREEN) {
    topParchment.hidden = NO;
    endSongButn.hidden = NO;
}

(Though, the recommended way of dealing with varying screen sizes is to use Auto Layout. See also Ray Wenderlich's Auto Layout Tutorial.)

Device Model

If you need to detect whether the current device is an iPhone, iPod, or iPad, use the UIDevice class's model method:

NSLog(@"Current device: %@", [[UIDevice currentDevice] model] );

Use caution when looking for "iPhone" in the device string - you don't want to exclude the iPod touch! Here are several macros that might be handy (save these to your Appname-Prefix.pch file to make them globally available):


#define IS_WIDESCREEN ( [ [ UIScreen mainScreen ] bounds ].size.height == 568 )
#define IS_IPHONE     ( [[[UIDevice currentDevice] model] rangeOfString:@"iPhone"].location != NSNotFound )
#define IS_IPAD       ( [[[UIDevice currentDevice] model] rangeOfString:@"iPad"].location != NSNotFound )

#define IS_IPHONE5    ( !IS_IPAD && IS_WIDESCREEN )

Retina Display?

To determine if the device has a retina display:

CGFloat screenScale = [[UIScreen mainScreen] scale];

Non-retina devices have a scale of 1.0. Retina devices have a scale of 2.0.

Additional References:


TopHome