I'm having trouble with a layer hosting view. This version of initWithFrame:
works as expected (the view is filled black) providing 'Wants Core Animation Layer' is selected in Interface Builder:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[self setWantsLayer:YES];
[[self layer] setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
[[self layer] setContents:[NSImage imageNamed:NSImageNameBonjour]];
}
return self;
}
I want to be able to configure the view programmatically so I want to remove the reliance on Interface Builder. Below is the second version which is meant to achieve this. This doesn't work. The view stays the same color as the parent view:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
CALayer *rootLayer = [CALayer layer]; //Added this line,
[self setLayer:rootLayer]; //and this line only
[self setWantsLayer:YES];
[[self layer] setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
[[self layer] setContents:[NSImage imageNamed:NSImageNameBonjour]];
}
return self;
}
I copied the code from the documentation and searched the web and all examples I found did exactly this, but it's not working!
What am I doing wrong?