user1295948
user1295948

Reputation: 303

cocoa : how to set background color of a view as light green color in cocoa without using CALayer?

I want to set the background color of the view without using CALayer.

Please help me out.

Upvotes: 1

Views: 2470

Answers (1)

Ryan Nichols
Ryan Nichols

Reputation: 308

You didn't specify if you're talking about AppKit (OSX) or UIKit (iOS). I'll asume you're talking about AppKit because most people coming from UIKit run into this.

NSView does not natively draw a background or background color. You have to either subclass NSView and draw your own color in drawRect, OR use NSBox provided in AppKit which does draw a background.

If you subclass NSView, then your drawRect method may look like this:

- (void) drawRect:(NSRect)dirtyRect {
    [[NSColor blackColor] setFill];
    NSRectFill(dirtyRect);
}

If you decide to use NSBox, then you can set the background color this way:

[myBox setFillColor:[NSColor blackColor]];

Upvotes: 5

Related Questions