views:

88

answers:

1

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.

A: 

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.

Laurent Etiemble
That's incorrect: the content view is just a view and is no more special than any other. You can easily replace a content view by creating (and properly setting the frame and autosizing behavior) and replacing via NSWindow's -setContentView: or by clicking the content view in the window (in the nib/xib) and setting the class name to that of your custom view. Works just fine and is done quite regularly.
Joshua Nozzi
You are right. What I meant by specially, is that you cannot define its frame or its auto-resizing flags.
Laurent Etiemble
Actually, there's one "special" difference: if you use the content view instead of a custom view, initWithCoder will be called instead of initWithFrame.
Dewayne Christensen