10

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.

1
  • It still solved what I was looking for, setting the background :) Commented Apr 5, 2016 at 13:28

7 Answers 7

9

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.

1
  • Thanks, but it has been allocated in a function called before this one.
    – WolfLink
    Commented Oct 6, 2011 at 7:35
3

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];
2
  • I am more thinking of something like [UIColor colorWithPatternImage:[UIImage imageNamed:@"Rope.png"]]; but I am starting simpler to try to eliminate possible factors to the problem.
    – WolfLink
    Commented Oct 6, 2011 at 7:38
  • If you are going to use colorWithPatternImage: you may find leak.
    – Anand
    Commented Oct 7, 2011 at 4:26
2

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];
3
  • No problem with the alpha. That isn't causing the issue. Just set it to what the OP has specified and it gives a light bluish color.
    – Bourne
    Commented Sep 27, 2011 at 3:17
  • I meant 0.5 instead of 50. Also, putting 50 wont cause it to crash, it will work the same as if you gave it 1.
    – WolfLink
    Commented Oct 6, 2011 at 7:37
  • @Wolflink your question didn't say it was crashing. If you're getting an exception you should have said so.
    – progrmr
    Commented Oct 6, 2011 at 12:54
0

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.

0

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.

0

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

-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.