Wednesday, August 15, 2012

Set Background Image for UIView


In iPhone SDK, there is no setBackgroundImage function for setting an background image of an UIView. But actually, this can be done by the setBackgroundColor function. The following code will set the background view to Red.
1self.view.backgroundColor = [UIColor redColor];
So if you want to set a picture as an background image of a view, just initialize an UIColorinstance with an image and set it to the view.
1UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"anImage.png"]];
2self.view.backgroundColor = background;
3[background release];
Please note that you have to set all the items which are in front of the background view toclearColor such that they will not cover the background image.

No comments:

Post a Comment