Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 598
YllierDev Avatar asked May 30 '26 07:05

YllierDev


2 Answers

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;
like image 107
Matt Avatar answered Jun 01 '26 21:06

Matt


I think what you want to be changing is the shadowRadius value - set that to zero and you should get the sharp edge you're looking for:

myView.layer.shadowRadius = 0;
like image 34
Rob Reuss Avatar answered Jun 01 '26 21:06

Rob Reuss