当背景颜色为透明色时,阴影不显示

问题描述 投票:23回答:3

我在我的xib中创建了一个uiview,背景颜色为清晰的颜色。当我在视图图层上应用阴影时,阴影不会出现。但是当我设置除了清晰颜色之外的背景颜色时,阴影就会显示出来。请帮忙。

这是我的代码

self.cView.layer.shadowColor=[UIColor whiteColor].CGColor;
self.cView.layer.shadowOffset=CGSizeZero;
self.cView.layer.shadowRadius=30.0;
self.cView.layer.shadowOpacity=1.0;
self.cView.layer.cornerRadius=10.0;
ios calayer
3个回答
29
投票

问题是,阴影实际上考虑了“上层”。如果它上面没有任何东西就没有阴影:How Shadows Work

编辑:

有这个食谱copied from paste bin

view.layer.shadowColor = [UIColor colorWithWhite:.5 alpha:1].CGColor;
view.layer.shadowRadius = 4.0f;
view.layer.shadowPath = CGPathCreateWithRect(CGRectMake(0, 0, 50, 50), NULL);
view.layer.shadowOpacity = 1.0f;
view.layer.shadowOffset = CGSizeMake(1, 1);

但是我怀疑这对你有什么用处:结果是一个“画”的视图,它带有阴影的颜色和周围的阴影。


3
投票

如果指定shadowPath属性(例如shadowView.layer.shadowPath = UIBezierPath(roundedRect: shadowView.bounds, cornerRadius: 10).cgPath),它甚至可以使用.clear backgroundColor。


0
投票

难道你不忘记将self.cView.clipToBounds设置为NO

© www.soinside.com 2019 - 2024. All rights reserved.