UIButton shadow on all 4 sides

人走茶凉 提交于 2019-12-21 20:17:20

问题


I am trying to generate shadow for a UIButton.

Below is what 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 generating shadow on right and bottom.

Is there way where I can have shadow on all 4 sides?

As an another solution, I am do this by putting image with shadow behind the button, but I don't want to go that way.

Is there any way to get this done programmatically?

Something like below.


回答1:


Since you offset the shadow by {4, 4} the shadow appears on the bottom-right side of the button. You could set a zero offset :

myButton.layer.shadowOffset = CGSizeZero

and by tweaking the shadowRadius you might achieve what you want.

Here is how a shadow is built:

(1st row) start from your button's shape

(2nd row) draw a black shape underneath your button and translate it by the amount specified in shadowOffset : 10px on the left, 0px on the right. On the right one you can't see the black rectangle as it's directly underneath the button

(3rd row) blur the black rectangle by the amount specified in blurRadius. Zero means no blur and the black rectangle would stay sharp, so if you don't offset and don't blur, you'll see nothing.



来源:https://stackoverflow.com/questions/27038559/uibutton-shadow-on-all-4-sides

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!