Leonardo Murri Leonardo Murri - 2 months ago 13x
Objective-C Question

Objective C - OS X - Issue adding NSShadow to NSImageView

I am trying to add a shadow to a NSImageView on an MAC application.

I created a custom NSImageView class "ShadowView.h" and modified the drawRect: like so:

- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];

NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowBlurRadius:5];
[shadow setShadowOffset:NSMakeSize(30.0, 3.0)];
[shadow setShadowColor:[NSColor redColor]];
[shadow set];

[self setWantsLayer:YES];
[self setShadow:shadow];

However nothing happens. Also, when I debug I can see the above code being called. I looked at this question from 5 years ago but it seems to not work anymore

Adding a Shadow to a NSImageView

Thank you!


When adding a shadow to a view, that view's superview also needs to have layer-backing enabled. If it doesn't, the view's shadow gets clipped at its own bounds, as seen in this sample app:

clipped shadow

Make sure you call -setWantsLayer:YES on your view's superview (or check the "Core Animation Layer" checkbox in Interface Builder) in order to make sure the shadow is completely visible:

full shadow