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.

Getting started with iOS

This is a quick an simple guide to writing code for iOS. This is a very simple Hello World application that is all written programmatically. This means no using the Storyboard or Interface Builder.

It always good to learn how to program without using the visual aids. As good as Storyboard and Interface Builder are being able to code without the need to use them is really useful and enables you to do a lot more advanced coding.

Starting with XCode create a new project and Select single view application

You will not need the ViewController.xib so you can delete this. Open the AppDelegate.m

Change the line:
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

and replace it with
self.viewController = [[ViewController alloc] init];

This will stop the application from looking for the xib (Interface builder file).

Open ViewController.m then in -(void)viewDidLoad firstly we will set a background colour for the view. For this we will set it as a standard blue colour.

self.view.backgroundColor = [UIColor blueColor];

Now we need to create a label to say Hello World. We will call this label HelloLabel. We will put this label 10 pixels in from the left and 100 pixels down from the top. We will make this label 300 pixels wide and 25 pixels high.

UILabel *HelloLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,100,300,25)];

Now we will set the text to say Hello World, set the colour to white and its text alignment to centre. We will also remove any background colour from the label so that the view background colour shows through. Finally we will set the font size to 20 pixels.

HelloLabel.text = @"Hello World";
HelloLabel.textColor = [UIColor whiteColor];
HelloLabel.textAlignment = NSTextAlignmentCenter;
HelloLabel.backgroundColor = [UIColor clearColor];
HelloLabel.font = [UIFont systemFontOfSize:20];

Finally we need to add the label to the view so that it becomes visible.

[self.view addSubview:HelloLabel];

Now when you run the application you will see a blue screen with a label saying Hello World in white text.

Why not try playing around with different colour or font sizes. You could also try changing the positioning of the label or the text it displays.