I'm attempting to make a drop shadow for a custom NSView subclass.
So far, I've managed:
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
NSShadow *dropShadow = [[NSShadow alloc] init];
[dropShadow setShadowColor: [NSColor redColor]];
[self setWantsLayer: YES];
[self setShadow: dropShadow];
}
return self;
}
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor blueColor] setFill];
NSRectFill(dirtyRect);
[super drawRect: dirtyRect];
}
which only renders a blue square (i.e. no shadow).
Am I setting up the drop shadow in the right place?
Am I meeting all of the necessary requirements for the use of setShadow:?
A few notes before answering the question:
super's implementation ofdrawRect:on a vanillaNSView. The default implementation does nothing.[self bounds]as the fill rectangle, notdirtyRect. ThedirtyRectparameter is used to indicate the part of the view that needs drawing and is used for drawing optimisation only.dropShadowobject. You should either callautoreleaseon it after creation or callreleaseon it after callingsetShadow:.The reason that the shadow isn't displaying are twofold. Firstly, in order for layer-backed views to display a shadow, the view's superview must also be layer-backed.
Secondly, you're setting the shadow's color but not its other parameters: