Coding for different screen sizes

One of the key things to remember when starting out programming for iOS is that there are different devices with different screen sizes to deal with.

The good news is that you don’t have to deal with many different screen sizes, at present there is the traditional iPhone size of 320 x 480 which is for the iPhone 4S and earlier, 768 x 1024 for the iPad and 320 x 568 for the iPhone 5.

The following will get you the bounds of the screen

CGRect screenBounds = [[UIScreen mainScreen] bounds];

You can then use this to get the height of the screen using the following:

CGFloat screenHeight = screenBounds.size.height;

From the height we can then determine which device it is

if(screenHeight == 480)
{
     NSLog(@"classic iPhone");
}
if(screenHeight == 568)
{
     NSLog(@"iPhone 5");
}
if(screenHeight == 768)
{
     NSLog(@"iPad");
}

 

This can then be used to adapt the layout of your app to fit the device that it has been installed on.