UIView shadow not working

為{幸葍}努か 提交于 2020-01-03 07:21:09

问题


This is my code.

listView.layer.masksToBounds = NO;
listView.layer.shadowOffset = CGSizeMake(-3, 3);
listView.layer.shadowColor=[[UIColor blackColor] CGColor];
listView.layer.shadowRadius = 4;
listView.layer.shadowOpacity = 1.0;
[listView.layer setShouldRasterize:YES];

It works good with shadow effect.

While changing

listView.layer.masksToBounds = YES;

I didnt get shadow effect.


回答1:


The shadow is actually drawn below the UIView. If you set maskToBounds to YES, this clips any drawing outside of the UIView frame. Here is a SO link that describes this in more detail.




回答2:


The shadow is drawn outside of the layer's bounds. You have to set listView.layer.masksToBounds = NO to see the shadow

if you set listView.layer.masksToBounds = YES you can't draw anything out side of bounds so you can not get shadow




回答3:


If you have to use masksToBounds = YES; check out this SO post. It tells you how to use both shadows and rounded corners (in this particular case) on a view by using two nested views: the outer view casts the shadow and does not mask to bounds while the inner view has rounded corners and masks to bounds.



来源:https://stackoverflow.com/questions/10384207/uiview-shadow-not-working

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