Show Menu

Do you have a job opening?

Submit your 30 day Job Listing for FREE

This is a quick iOS Development tip that will show you two different ways that you can set the background image of you application

So, There are two ways to set a background image to a UIView –

  • You can set your view background color to color created with UIColor’s colorWithPaternImage.
  • You can add a UIImageView subview to your view.

colorWithPaternImage is ideal when you wish to use a small image/pattern, say about 5px by 5px as a repeated background in your iOS Application. This method is not recommended when using a large image. For that you should use a UIImageView instead otherwise you would consume too much memory and potentially cause your iOS Application to crash.

You would need to drag the pattern image into your project and then you could use:

self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backgroundImage"]];

If you are using the UIImageView then you would use:

UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"background"]];
[self.view addSubview:backgroundView];

Adding Background Images to the UIKit Controls

In iOS5, Apple released their Appearance API which allows you to bring appearance changes to the UIKit Controls. This means that you can now set the tint color, background image, and title position properties and others on a wide variety of objects, including toolbars, navigation bars, search bars, buttons, sliders, and other controls. A pretty neat addition right?

So, lets say you wanted to add the background image to the Navigation Bar, you could achieve this using the following piece of code:

[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bar"] forBarMetrics:UIBarMetricsDefault];

For further reading into the Appearance API Have a look at the apple documentation

if, you’re new to iOS Application Development then why not check out this great resources:

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly

Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.

Written by:

I work in the shadows. You can thank me by being a good soul.