Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlaying UIView with an background image

Is it possible to overlay UIView with an image without using UIImageView from the Interface Builder?

If not, how would you accomplish the same in code?

like image 394
James Raitsev Avatar asked Oct 29 '11 22:10

James Raitsev


2 Answers

From the UIView documentation:

Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.

Using the second option, to set someView to have a background of yourImage.png:

someView.layer.contents = (id)[[UIImage imageNamed:@"yourImage.png"] CGImage];

like image 144
dingo sky Avatar answered Sep 29 '22 08:09

dingo sky


if by Overlying a UIView you mean painting its background with an image, it can be done like this:

someView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage.png"]];

You could also add a new UIImageView that covers your UIView by code, Init your UIImageView with the same frame Rect as your UIView and add it as a subview.

like image 40
Alon Amir Avatar answered Sep 29 '22 07:09

Alon Amir