I've successfully implemented drawing a shadow around my UIView
like this:
block1.layer.masksToBounds = NO;
block1.layer.shadowOffset = CGSizeMake(0, 0);
block1.layer.shadowRadius = 1;
block1.layer.shadowOpacity = 0.7;
What now happens is I have a rectangular UIView
and i would like to draw the shadow around it three sides, leaving the bottom side of it without the shadow.
I know that I have to specify the block1.layer.shadowPath
by creating a new UIBezierPath
but I'm not sure how to do it.
Obviously, setting layer.shadowOffset
won't do the trick for me.
Thanks in advance!
Try this
A bit of improvement for other answers, thanks to Ashok R for swift code.
Since we were creating a triangular view in the background of the view with shadow on all sides, and a white triangle on the sides shadow is not needed.
It breaks in case of views with width comparatively larger than height.
A workaround will be to shift the path for the line where shadow is not needed a bit towards that side of view, instead of creating the triangular view Path completely.
I have created an extension for that -
and set the boolean true for whichever side you want the shadow to appear
myView.addshadow(top: false, left: true, bottom: true, right: true, shadowRadius: 2.0)
or
myView.addshadow(top: true, left: true, bottom: true, right: true, shadowRadius: 2.0)
or
myView.addshadow(top: false, left: false, bottom: true, right: true, shadowRadius: 2.0)
Updating Ryan Poolos Answer to Swift 3.0
Thanks to Ryan Poolos
Result:
I know you say setting
layer.shadowOffset
won't work for you, but you are allowed to put in negative values so setting itlayer.shadowOffset = CGSizeMake(0.0, -2.0)
would come close to the effect you're looking for but of course I expect you want it to be even on the three sides.So here we go with
layer.shadowPath
!And to give you an idea of whats going on, here is the actual shadow path you just drew :)
Its possible to just shift that extra middle point before or after the other lines to choose which side will be omitted.