UIButton shadow on all 4 sides

I am trying to create a shadow for a UIButton.

Below I am using.

myButton.layer.shadowColor = [UIColor blackColor].CGColor;
myButton.layer.shadowOpacity = 0.5;
myButton.layer.shadowRadius = 1;
myButton.layer.shadowOffset = CGSizeMake(4, 4);
myButton.layer.masksToBounds = NO;

      

But its a generating shadow on the right and below.

Is there a way that I can have shadow on all four sides?

As another solution, I do it by placing the shadowed image behind the button, but I don't want to go that route.

Is there a way to do this programmatically?

Something like below.

enter image description here

+3


source to share


1 answer


As you offset the shadow by {4, 4}

, the shadow appears at the bottom right of the button. You can set the offset to zero:

myButton.layer.shadowOffset = CGSizeZero

      

and by customization shadowRadius

you can achieve what you want.

This is how the shadow is constructed:



(1st line) start with your button shape

(second line) draw a black shape below your button and translate it to the amount shown in shadowOffset

: 10px left, 0px right. On the right, you don't see the black rectangle as it is right below the button

(3rd line) blurs the black rectangle by the amount specified in blurRadius

. Zero means there is no blur, and the black rectangle will remain sharp, so unless you sweep and blur, you won't see anything.

enter image description here

+6


source







All Articles