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.
Solution for Swift 3.0
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "home02.png")!)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With