1
votes

I'm trying to write a custom NSView that supports adding a background colour. I override drawRect

- (void)drawRect:(NSRect)rect {

    [[NSColor blackColor] set];  
    //[NSBezierPath fillRect:rect]; //I tried this too  
    NSRectFill([self bounds]);  

}

And I set my custom view type to replace the content view NSView of my main window. But when I run it, the background does not turn black as expected - but some contained subviews do.

What I am doing wrong? My draw method is definitely getting called, but background does not change.

1
1 - How are you replacing the content view with your custom view? 2 - How do you know your custom view's -drawRect: is being called?Joshua Nozzi
There's a good chance your method is not being called. Try putting a breakpoint in your -drawRect: method and see if it is hit. if not, recheck your UI setup (code or nib) to see why your class is not being used.Todd Ditchendorf

1 Answers

0
votes

Do not replace the content view of your window as it is treated specially. Add your custom view to the content view, and set the autoresize flags instead.