简体   繁体   中英

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.

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]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM