Ios – Setting the background color of a UIVIew

iosiphoneobjective cuiview

EDIT: People keep visiting this post which doesn't have much good information so I'll put this here to help you guys:
Try setting the backgroundColor property of your UIView. For example:

myView.backgroundColor = [UIColor blueColor];

Make sure your view has been instantiated and if it is controlled by an xib or storyboard at all, make sure that the view has already been loaded (otherwise whatever is set in interface builder will become the background color). If that doesn't work, something weird is going on. Keep looking, this post won't help you.

Original post:

I have an NSObject with a UIView property. Mostly I use this UIView for displaying a picture, but in some cases, I want to set the background color of it. However, each time I try to set the background color, the color stays as clear. Any images or other subviews appear but the background does not. How can I change the background color of a UIView? Here is some of my code:

here is an example where I need a tiled image:
(this is in a function called after I add the UIView to the viewcontroller's view)

(picture is a UIView Property)

 picture.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Tile.png"]];

I have also tried in other cases:

picture.backgroundColor = [UIColor colorWithRed:0 green:1 blue:1 alpha:0.5];

In both cases, the background remains clear.

Best Answer

Are you allocating and initializing the UIView mentioned above? The problem should be that you have most likely forgotten to do that for the UIView called picture.

Just try:

picture = [[UIView alloc] initWithFrame:CGRectMake(10,10,200,200)];
picture.backgroundColor = [UIColor redColor];

Assuming that picture has already been declared an iVar of type UIView in your interface file.