lab12
lab12

Reputation: 6448

Create Drop Shadow for NSView - Cocoa

I'm trying to create a drop shadow surrounding the NSView like how NSWindow does it with its shadow, but I'm having some difficulty. I created a class for the NSView I'm creating the drop shadow for and I'm using this code for the overriding method:

   -(void)drawRect:(NSRect)dirtyRect {
    NSRect rect = NSInsetRect([self bounds], 10.0, 10.0);
    NSShadow *dropShadow = [[[NSShadow alloc] init] autorelease];
    [dropShadow setShadowColor:[NSColor blackColor]];
    [dropShadow setShadowBlurRadius:5];
    [dropShadow setShadowOffset:NSMakeSize(0,-3)];

    [NSGraphicsContext saveGraphicsState];

    [dropShadow set];

    NSRectFill(rect);

    [NSGraphicsContext restoreGraphicsState];

    [super drawRect:dirtyRect];
}

This doesn't really create a drop shadow in which I'm looking.

Here is the shadow I'm trying to aim for...

NSWindow Shadow

rather creates a line through the NSView that seems like a border within the bounds of the view. Anyone got any ideas for this?

Upvotes: 2

Views: 3129

Answers (1)

Sathish Kumar
Sathish Kumar

Reputation: 229

I have faced similar shadow issues because NSView clips its bounds.

I fixed it when I used a layer backed view. I simply set the superview's wantsLayer property to YES.. i.e [[view superView] setWantsLayer:YES] and set shadow for view [view setShadow:dropShadow].

Upvotes: 3

Related Questions