0

我正在使用以下代码使用 calayer 设置我的 nsview 的背景颜色。

    CALayer *viewLayer = [CALayer layer];
    [self.view setLayer:viewLayer];
    [self.view setWantsLayer:YES]; 
    [viewLayer setBackgroundColor:CGColorCreateGenericRGB(0.74, 0.99, 0.79, 1.0)];

此代码使我的应用程序崩溃。

请帮我..

4

1 回答 1

0

你的代码对我有用。您必须检查的唯一一件事是您的视图在您为其设置图层时已加载。

如果您只想绘制具有指定背景颜色的视图,则不必为其设置 CALayer。最简单的方法是创建自己的NSView子类:

@interface MyColoredView: NSView

@property (copy) NSColor* backgroundColor;

@end

@implementation MyColoredView

@synthetize backgroundColor;

- (void)drawRect:(NSRect)dirtyRect
{
    [self.backgroundColor set];
    NSRectFill(dirtyRect)
}

@end

之后,您必须在 IB 中为您的视图设置类,MyColoredView并在初始化期间设置它的背景颜色:

self.view.backgroundColor = [NSColor greenColor];
于 2012-08-07T11:35:33.587 回答