开发者

Setting the background color of a UIVIew

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.backgro开发者_运维百科undColor = [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.


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.


If you are good in using RGB value you can use

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

Otherwise use UIColor to set the color for backgound

picture.backgroundColor = [UIColor greenColor];


alpha has a valid range of 0.0 to 1.0. You have it set to 50, try 1 instead:

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


I'm not sure what the problem was, although I do know it was not one of the problems you have answered with, but last time I went to work on the problem I noticed that it was working. Thank you for your help anyway.


It's probably worth checking whether you're changing the background color on the main thread or not. If you change the background color in a dispatch queue (other than the main queue) it can sometimes not be reflected in the UI appearance.


Can use attribute inspector for change color. Select color for 'Background'.


Your code seems totally valid and it should change the background colour. You may want to double check that picture.opaque property is set to YES (it's the default value unless your code change it). But a more likely problem is that the frame of the view has zero size. I would advise to verify this. For example, by logging it:

    NSLog(@"Frame size: width=%f, height=%f", picture.frame.size.width, picture.frame.size.height);

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜