I’m working on a Cocoa project using Core Animation and I’ve got a custom view that is displayed in two windows. It always shows up in one window, but sometimes does not show up in the other window when I start up the application. So far as I can tell, it is completely random. Here is the code I call when the view is initialized. It gets to this code whether or not the view appears.
[self setWantsLayer:YES];
root = [self layer]; // root is a CALayer
root.layoutManager = [CAConstraintLayoutManager layoutManager];
root.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
[root setBackgroundColor:CGColorGetConstantColor(kCGColorBlack)];
[self setNeedsDisplay:YES];
Why would the view show up sometimes and other times it does not?
EDIT: Would it make a difference if I create the root CALayer on it’s own instead of setting it to the view’s “layer” like I’m currently doing?
Looks like there was a pretty simple solution, but it was not well documented. Instead of setting root to the sub-classed view’s layer, I create root as a new CALayer and then set the view’s layer to root. The code from the original question now looks like:
I’m thinking that sometimes when my initialization code was called, the view had not initialized the layer associated with itself, so root was not getting properly assigned. This is just a hunch, but making the above changes has resolve my problem with the view not always displaying.