Andraz
Andraz

Reputation: 152

Draw NSView background partially, with a gradient

I have a NSView, subclassed, with custom background drawing, filling it with a gradient. In IB, I've put a checkbox on it, somewhere in the middle.

This is the drawRect method.

-(void) drawRect:(NSRect)dirtyRect {
    CGFloat sc = 0.9f;
    CGFloat ec = 0.6f;  
    NSColor* startingColor = [NSColor colorWithDeviceRed:sc green:sc blue:sc alpha:1];
    NSColor* endingColor = [NSColor colorWithDeviceRed:ec green:ec blue:ec alpha:1];
    NSGradient *grad = [[NSGradient alloc] initWithStartingColor:startingColor endingColor:endingColor];
    [grad drawInRect:dirtyRect angle:270];
}

What happens is, this same method gets called to draw the whole view area first and then for the part, where NSButton (checkbox) lies on top of it. OF course the checkbox background is drawn with a complete gradient and it is not right, since the portion is much smaller. The same happens with other controls I put on the said NSView.

What is the suggested approach on such thing?

One option is to make controls height the same as the views' but this will result in problems in the future.

Upvotes: 0

Views: 1409

Answers (1)

Andraz
Andraz

Reputation: 152

The answer is, always draw the WHOLE area of the view, not just the dirtyRect

[grad drawInRect:[self bounds] angle:270];

Upvotes: 2

Related Questions