How to programmatically configure an NSView layer

What is the right way to do this? Here is what I am trying. But the display is never called on dotLayer:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    DotLayer *dotLayer = [[DotLayer alloc]init ];
    dotLayer.frame= CGRectMake(10, 10, 100, 100);
    dotLayer.nDots = 4;
    NSView *contentView = window.contentView;
    CALayer *layer = [[CALayer alloc]init];
    layer.frame = CGRectMake(0,0,200,200);
    contentView.layer = layer;
    [layer addSublayer:dotLayer];
    [dotLayer setNeedsDisplay];
}

DotLayer is a subclass of CALayer.

+3
source share
1 answer

Did you forget to install WantsLayer, maybe?

[contentView setLayer:.. whatever your root layer is];
[contentView setWantsLayer:YES];

(Remember that the ORDER of these two lines is critical ... see doco.)

In Swift it is

contentView.wantsLayer = true
+8
source

Source: https://habr.com/ru/post/1783336/


All Articles