Search code examples
ioscore-graphicsshadowdropshadow

iOS: drop shadow with sharp edges


I'm trying to add some shadows to one of my views and what I would like to achieve is drawing shadows only on one side and let them have sharp edges. No I've tried quite a few methods without any luck (using the shadow related properties of the view's CALayer + UIBezierPaths). However, iOS is always rendering a shadow with soft edges like this:

enter image description here

But what I really want to acchieve is something like this (without round corners and sharp edges on the sides except one):

enter image description here

Is there any elegant way to do this or will I have to draw the shadow myself using CoreGraphics?

PS: I forgot to mention, my view should actually be a custom UIButton, so overriding drawRect: would be a pain here


Solution

  • I've experienced a mask removing the shadow from view...so maybe you can try that.

        CALayer *mask = [[[CALayer alloc] init] autorelease];
        mask.backgroundColor = [UIColor blackColor].CGColor;
        mask.frame = CGRectMake(0.0, 0.0, yellowView.bounds.size.width + shadowWidth, yellowView.bounds.size.height);
        yellowView.layer.mask = mask;