ios 在 UIView 顶部添加阴影
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24128355/
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
Adding shadow to top of UIView
提问by Apollo
How can I add a shadow to the top of my UIView
I've tried the following but with no luck...
如何在我的顶部添加阴影UIView
我尝试了以下但没有运气......
childView.layer.cornerRadius = 5;
childView.layer.masksToBounds = YES;
childView.layer.shadowOffset = CGSizeMake(-15, 20);
childView.layer.shadowRadius = 5;
childView.layer.shadowOpacity = 0.5;
回答by Ryan Herubin
Swift 3 Extension:
斯威夫特 3 扩展:
This includes default values for the app I'm working on, but you can change them to match the style you want in your app.
这包括我正在处理的应用程序的默认值,但您可以更改它们以匹配您想要的应用程序样式。
enum VerticalLocation: String {
case bottom
case top
}
extension UIView {
func addShadow(location: VerticalLocation, color: UIColor = .black, opacity: Float = 0.5, radius: CGFloat = 5.0) {
switch location {
case .bottom:
addShadow(offset: CGSize(width: 0, height: 10), color: color, opacity: opacity, radius: radius)
case .top:
addShadow(offset: CGSize(width: 0, height: -10), color: color, opacity: opacity, radius: radius)
}
}
func addShadow(offset: CGSize, color: UIColor = .black, opacity: Float = 0.5, radius: CGFloat = 5.0) {
self.layer.masksToBounds = false
self.layer.shadowColor = color.cgColor
self.layer.shadowOffset = offset
self.layer.shadowOpacity = opacity
self.layer.shadowRadius = radius
}
}
回答by Mischa
You need to set the childview's masksToBounds
property to NO
in order to make the shadow visible.
您需要将子视图的masksToBounds
属性设置NO
为 以使阴影可见。
childView.layer.masksToBounds = NO;
回答by joels
Set your masksToBounds = NO
. The reason you are not seeing the shadow is because it is completely hidden behind your view when the masksToBounds is YES.
设置您的masksToBounds = NO
. 您没有看到阴影的原因是,当 maskToBounds 为 YES 时,它完全隐藏在您的视图后面。
If your button is rounded you can instead adjust the view's imageEdgeInset value. ie: UIEdgeInsetsMake(5, 0, 0, 10);
如果您的按钮是圆形的,您可以改为调整视图的 imageEdgeInset 值。IE:UIEdgeInsetsMake(5, 0, 0, 10);
回答by ProgrammingProton
1.Step: First create an outlet of uiview
1.步骤:首先创建一个uiview的outlet
2.Step:
2.步骤:
buttonBackgroundView.layer.shadowOpacity = 0.7
buttonBackgroundView.layer.shadowOffset = CGSize(width: 3, height: 3)
buttonBackgroundView.layer.shadowRadius = 15.0
buttonBackgroundView.layer.shadowColor = UIColor.black.cgColor
回答by Literphor
Try setting masksToBounds to NO. According to this link Whats the best way to add a drop shadow to my UIViewmaking that YES will clip the layers extending past the view's bounds.
尝试将masksToBounds 设置为NO。根据此链接,将阴影添加到我的 UIView 的最佳方法是什么,使 YES 将剪辑延伸超过视图边界的图层。