2012-08-07 75 views
0

我使用下面的代码来设置我的nsview使用calayer的背景颜色。cocoa:使用calayer会让我的应用程序崩溃吗?

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

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

请帮我..

+0

当和你在哪里调用这个代码? – Dmitry 2012-08-07 10:52:26

+0

在应用程序初始化期间。 – user1295948 2012-08-07 11:07:37

+0

有时会起作用,有时不起作用。 – user1295948 2012-08-07 11:08:00

回答

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]; 
相关问题