Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewController background Image

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.

like image 498
CodeGuy Avatar asked Aug 13 '10 16:08

CodeGuy


2 Answers

Solution for Swift 3.0

self.view.backgroundColor = UIColor(patternImage: UIImage(named: "home02.png")!)
like image 128
santhoshkumar Avatar answered Oct 13 '22 23:10

santhoshkumar


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.

like image 35
viggio24 Avatar answered Oct 14 '22 00:10

viggio24