I have recently converted one of the views in my OS X App to be layer-hosted and all is working well under Mountain Lion, however one of my testers is complaining that the layers aren't showing under Snow Leopard. I have written a small test app to perform further tests (source code here), and this test app also doesn't work under 10.6.
Here is the main body of code that sets-up the layers:
- (id)initWithFrame:(NSRect)frameRect
{
NSLog(@"initWithFrame");
self = [super initWithFrame:frameRect];
if (self != nil)
{
srand((unsigned)time(NULL));
_rootLayer = [[CALayer alloc] init];
_rootLayer.delegate = self;
_rootLayer.anchorPoint = CGPointMake(0.0, 0.0);
_rootLayer.frame = NSRectToCGRect([self bounds]);
_rootLayer.needsDisplayOnBoundsChange = NO;
_rootLayer.masksToBounds = YES;
self.layer = _rootLayer;
self.wantsLayer = YES;
_backgroundLayer = [[CALayer alloc] init];
_backgroundLayer.delegate = self;
_backgroundLayer.anchorPoint = CGPointMake(0.5, 0.5);
_backgroundLayer.frame = CGRectInset(NSRectToCGRect([self bounds]), BACKGROUND_INSET, BACKGROUND_INSET);
_backgroundLayer.cornerRadius = 5.0;
_backgroundLayer.needsDisplayOnBoundsChange = NO;
_backgroundLayer.masksToBounds = YES;
[_rootLayer addSublayer:_backgroundLayer];
_mouseLayer = [self _createOtherLayer];
_mouseLayer.opacity = 0.5;
for (unsigned i = 0; i < NUM_OTHER_LAYERS; i++)
_otherLayers[i] = [self _createOtherLayer];
[_backgroundLayer addSublayer:_mouseLayer];
[_rootLayer setNeedsDisplay];
[_backgroundLayer setNeedsDisplay];
[self _positionOtherLayersInRect:frameRect];
_trackingArea = nil;
[self updateTrackingAreas];
}
return self;
}
And here is the method that creates the other layers:
- (CALayer *)_createOtherLayer
{
CALayer *layer = [[CALayer alloc] init];
layer.delegate = self;
layer.anchorPoint = CGPointMake(0.5, 0.5);
layer.bounds = CGRectMake(0.0, 0.0, 64.0, 64.0);
layer.position = CGPointMake(0.0, 0.0);
layer.needsDisplayOnBoundsChange = NO;
layer.masksToBounds = YES;
layer.shadowColor = CGColorGetConstantColor(kCGColorBlack);
layer.shadowOffset = CGSizeMake(2.0, -2.0);
layer.shadowRadius = 2.0;
layer.shadowOpacity = 1.0;
[_backgroundLayer addSublayer:layer];
[layer setNeedsDisplay];
return layer;
}
Can anyone suggest why these layers don't work under 10.6?
Have you tried moving the code in initWithFrame: into awakeFromNib? It seems to be a common enough mistake that causes the layers to get screwed up. In this question the problem was that the layers were set up in initWithFrame, but since nibs are marked by default as not needing layers, they were wiped out immediately after. Move the code to awakeFromNib, and instead of using the passed frame use self.frame and see if that fixes the problem. At the very least it shouldn't be any worse (running on my Mac running Lion after moving the code to awakeFromNib and it still works fine, so it didn't break anything), and it may just be the solution you're looking for.
Hopefully this works, or you find another solution soon. Have a good day. :)