I have an NSView whose layer has various sublayers.
Its needsDisplay property is set to YES when the app changes its effective appearance, which ends up calling:
-(void)updateLayer {
// this is a simplified version
sublayer1.backgroundColor = NSColor.windowBackgroundColor.CGColor;
sublayer2.content = [NSImage imageNamed:@"dynamic image"];
// "dynamic image" is an image asset that has light and dark variants.
}
Both layers render properly according to the theme after the app launches, but when the app changes theme (without being relaunched), the appearance of sublayer2 does not change. The appearance of sublayer1 does change, but I have to relaunch the app for sublayer2 to update to the app theme.
Using "dynamic image" as template image or original image has no effect on this issue.
Note that the same image used in an NSButton does change appearance properly without having to relaunch the app.
Any suggestion?
From Apple's docs for
CALayer.contents:So, when setting:
the layer gets a
CGImageRef, not aNSImage... and appearance changes will have no effect.One approach would be to replace the
CALayerwith aNSImageViewas a subview. Set the.imageproperty, and you're done.If you need to use
CALayer, we need to get theCGImageReffrom the image with the current appearance inupdateLayer.Quick example:
LayeredView.hLayeredView.m