setBackgroundColor not working in drawRect


#1

Hi guys,

This code is a excerpt from the view HypnosisView.m file. I was expecting drawRect to change the view’s background colour to Green but it does nothing. Am I missing a crucial concept? I have commented the code part i don’t understand below. I’ve only put the relevant code below.

#import "HypnosisView.h"
@implementation HypnosisView
@synthesize circleColor;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
         [self setBackgroundColor:[UIColor blueColor]]; //Works fine
         [self setCircleColor:[UIColor yellowColor]]; //Works fine
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    
    NSLog(@"In drawRect");
    
    [self setBackgroundColor:[UIColor greenColor]]; // This has NO effect and the background colour stays at blue
    [self setCircleColor:[UIColor orangeColor]]; // This DOES have an effect and the circle colour changes to orange
}

Thanks in advance for your help and for creating a great IOS book!!


#2

You are not actually doing anything (creating a rectangle) inside your drawRect:rect method, apart from displaying a NSLog statement. Within this chapter you are actually shown how to set the fill colour of the rectangle.


#3

Thanks for looking Fujilla. However I’ve stripped out the code that is not relevant to the question I’m asking - ( I have completed the chapter successfully already)

It does more than print an NSLOG statement as below it in the drawRect you will see the code:

[self setBackgroundColor:[UIColor greenColor]]; // This has NO effect and the background colour stays at blue
[self setCircleColor:[UIColor orangeColor]]; // This DOES have an effect and the circle colour changes to orange

I’m just wondering why the background colour of the view doesn’t change when the setBackgroundColour message is sent to the view from drawRect.


#4

You can’t set the view’s background color in the drawRect: method. If you want to do that, you must set it in the initialiser. You can, however, set the fill color or stroke color in the drawRect: method:

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder];
    if (self) {
        [self setBackgroundColor:[UIColor greenColor]];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [[UIColor redColor] setFill];
    [[UIBezierPath bezierPathWithRect:CGRectInset (rect, 20, 20)] fill];
}

#5

Thanks ibex. I’m just trying to understand the actual reason why it cannot be set in the drawRect method - as the view is receiving the same message setBackgroundColor irrespective of whether the message is being sent from the within drawRect method or the initialiser.

Cheers
Zain


#6

It’ll probably show up with a blue background the next time you send it setNeedsDisplay. Probably.

The reason you don’t see it immediately is because, before drawRect: is even called, the background color is painted onto the image that represents the view. You are changing a value that was already used.