maskToBounds in OSX

Continuing my OSX CoreAnimation adventures I stumbled upon a really weird behaviour.
On iOS you can add any layer as a child and have it being completely rendered even if it is outside its parent frame by making sure its masksToBounds is set to NO (which is the default value):

self.view.layer.masksToBounds = NO;  
self.view.layer.backgroundColor = [UIColor     greenColor].CGColor;  
CALayer *blueLayer = [CALayer layer];  
blueLayer.frame = CGRectMake(self.view.frame.size.width - 50. , 0., 100., 100.);  
blueLayer.backgroundColor = [UIColor blueColor].CGColor;  
[self.view.layer addSublayer:blueLayer];

As you can imagine, by running this example you will have your blue box reaching out outside it's parent frame.

Now let's try to do this on OSX:

[self.view setWantsLayer:YES];
self.view.layer.masksToBounds = NO;  
self.view.layer.backgroundColor = [NSColor greenColor].CGColor;  
CALayer *blueLayer = [CALayer layer];  
blueLayer.frame = CGRectMake(self.view.frame.size.width - 50. , 0., 100., 100.);  
blueLayer.backgroundColor = [NSColor blueColor].CGColor;  
[self.view.layer addSublayer:blueLayer];

If you try to run this, you will see that the blue box will be masked by it's parent bounds so you won't be able to see it completely even though we set masksToBounds to NO.

After swizzling the setMasksToBounds method and having a conditional breakpoint in it that stops only when we are dealing with that particular layer we arrive at the following stacktrace:

swizzle_stacktrace

So, after a setFrame, the masksToBounds property is reset, thus ignoring our intention to turn it off.

How do we fix this?
In order to prevent AppKit from resetting masksToBounds whenever it wants to, we have to create our own CALayer and set it as the view's layer. Only after having done that can we say that we want a layer by setting wantsLayer to YES.
If we do that, AppKit acknowledges our control and doesn't mess with the layer:

CALayer* myLayer = [CALayer layer];  
myLayer.frame = self.custom_view.frame;  
[self.custom_view setLayer:myLayer];
[self.custom_view setWantsLayer:YES];
self.custom_view.layer.masksToBounds = NO;  
self.custom_view.layer.backgroundColor = [NSColor greenColor].CGColor;  
CALayer *blueLayer = [CALayer layer];  
blueLayer.frame = CGRectMake(self.custom_view.frame.size.width - 50 , 0., 100., 100.);  
blueLayer.backgroundColor = [NSColor blueColor].CGColor;  
[self.custom_view.layer addSublayer:blueLayer];