13

How do I set the background image of my UIViewController? What is the code to do it? Right now I have

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

but I don't want a pattern. I want it to fill the screen properly.

MMiroslav
  • 1,672
  • 20
  • 32
CodeGuy
  • 28,427
  • 76
  • 200
  • 317

5 Answers5

34

you could use:

UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Blah.png"]];

[self.view addSubview:backgroundImage];
[self.view sendSubviewToBack:backgroundImage];
[backgroundImage release];

Apple HD image Guide

Jesse Naugher
  • 9,780
  • 1
  • 41
  • 56
  • 1
    The iPhone 4 is 960-by-640-pixel resolution at 326 ppi according to Apple's website. When I make an image this large, it is way too big for a background when i try putting it on my phone using that code. How do I make it fit? I'm trying to make a high resolution background for one of my applications. – CodeGuy Aug 13 '10 at 16:41
  • ahh. I would try reading up on the point to pixel conversion (there is a guideline to this somewhere). I personally am not exactly sure how it works, but you would just have to adjust the UIImageViews frame perhaps? I personally have never used the HD resolution for iPhone4. – Jesse Naugher Aug 13 '10 at 16:47
  • What do you mean "points to pixel" conversion? Would you mind helping me find some guide on this? – CodeGuy Aug 13 '10 at 16:50
  • added a link in my answer to the apple guide on supporting HD images. – Jesse Naugher Aug 13 '10 at 17:00
  • 1
    This code will cause a memory leak. You'll need to add `[backgroundImage release];` as the last line of code here. – gotnull Aug 25 '11 at 05:10
  • @Fulvio: +1: true true..fixed :) – Jesse Naugher Aug 25 '11 at 13:53
  • The last line of code fixed an issue for me. Thanks! :D – Robert J. Clegg Apr 16 '15 at 11:46
7

self.view.layer.contents = (id)[UIImage imageNamed:@"background.png"].CGImage;

Reason: each view is backed by a CALayer class. According to documentation on CALayer:

contents
An object that provides the contents of the layer. Animatable.


@property(retain) id contents

Discussion
A layer can set this property to a CGImageRef to display the image as its contents. The default value is nil.

And yes, this property is animatable. This means that nice transitions based on CoreAnimation can be done on this layer. Note: be careful if you support multiple orientations.

viggio24
  • 12,316
  • 5
  • 41
  • 34
4

A swift solution for this...

let backgroundImageView = UIImageView(image: UIImage(named: "backgroundImage"))
backgroundImageView.frame = view.frame
backgroundImageView.contentMode = .ScaleAspectFill
view.addSubview(backgroundImageView)
view.sendSubviewToBack(backgroundImageView)
Urkman
  • 1,298
  • 1
  • 16
  • 32
2

Solution for Swift 3.0

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "home02.png")!)
santhoshkumar
  • 191
  • 2
  • 10
1

To expand on Jesse Naugher's code: UIView (or UIViewController, for that matter), holds no built-in way of using an image for background. What you have to do is to use a UIImageView, which is a UIView subclass specifically for displaying images, and add that to the back of your view hierarchy.

eliego
  • 2,279
  • 2
  • 18
  • 22
  • Thank you. I understand. Do you have any wisdom on the matter that I posted as a comment to Jesse Naugher's code? – CodeGuy Aug 13 '10 at 16:44