BadPirate BadPirate - 5 months ago 50
Objective-C Question

Best way to change the background color for an NSView

I'm looking for the best way to change the

backgroundColor
of an
NSView
. I'd also like to be able to set the appropriate
alpha
mask for the
NSView
. Something like:

myView.backgroundColor = [NSColor colorWithCalibratedRed:0.227f
green:0.251f
blue:0.337
alpha:0.8];


I notice that
NSWindow
has this method, and I'm not a big fan of the
NSColorWheel
, or
NSImage
background options, but if they are the best, willing to use.

Answer

Yeah, your own answer was right. You could also use cocoa methods:

- (void)drawRect:(NSRect)dirtyRect {
    // set any NSColor for filling, say white:
    [[NSColor whiteColor] setFill];
    NSRectFill(dirtyRect);
    [super drawRect:dirtyRect];
}
Comments