ios UIView 阴影不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10384207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:10:31  来源:igfitidea点击:

UIView shadow not working

iphoneobjective-ciosios5

提问by Anbu Raj

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.

我没有得到阴影效果。

回答by bbarnhart

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.

阴影实际上是绘制在 UIView 下方的。如果您将 maskToBounds 设置为 YES,这将剪辑 UIView 框架之外的任何绘图。 这是一个 SO 链接,更详细地描述了这一点。

回答by Hector

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

阴影绘制在图层边界之外。你必须设置listView.layer.masksToBounds = NO才能看到阴影

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

如果你设置了,listView.layer.masksToBounds = YES你就不能在边界之外画任何东西,所以你不能得到阴影

回答by Raginmari

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.

如果您必须使用,masksToBounds = YES;请查看此 SO 帖子。它告诉您如何通过使用两个嵌套视图在视图上同时使用阴影和圆角(在这种特殊情况下):外部视图投射阴影并且不屏蔽边界,而内部视图具有圆角和边界屏蔽。

回答by Vishnuvardhan

Only below worked for me

只有下面对我有用

[self.view bringSubviewToFront:subView];