Search code examples
ioscocoa-touchuicolor

How to avoid tiling when setting a view's backgroundColor?


I'm trying to set up a background image for my app, but the image is tiling. How do I prevent this? I want my image to fit to the background.

In CSS I would set NO-REPEAT; is there something like that?

I have tried this code:

-(IBAction)btnAddBackground
{
    self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"image.jpg"]];
}

Solution

  • The documentation for UIView makes a couple of suggestions for this:

    • 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.