Swift 3 xcode storyboard,给UIView添加box-shadow,就像CSS样式的box-shadow
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42244040/
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
Swift 3 xcode storyboard, add box-shadow to UIView, like a CSS style box-shadow
提问by Thomas Charlesworth
回答by Bhautik Ziniya
Set your view's shadow properties to add a shadow.
设置视图的阴影属性以添加阴影。
SWIFT 3
斯威夫特 3
YourView.layer.shadowOpacity = 0.7
YourView.layer.shadowOffset = CGSize(width: 3, height: 3)
YourView.layer.shadowRadius = 15.0
YourView.layer.shadowColor = UIColor.darkGray.cgColor
NOTE:replace YourView with the view you want shadow.
注意:用你想要阴影的视图替换 YourView。
回答by Pineapple3D
here is my approach to this:
这是我的方法:
You create a new class called ShadowView
.
您创建了一个名为 的新类ShadowView
。
import UIKit
@IBDesignable
class ShadowView: UIView {
//Shadow
@IBInspectable var shadowColor: UIColor = UIColor.black {
didSet {
self.updateView()
}
}
@IBInspectable var shadowOpacity: Float = 0.5 {
didSet {
self.updateView()
}
}
@IBInspectable var shadowOffset: CGSize = CGSize(width: 3, height: 3) {
didSet {
self.updateView()
}
}
@IBInspectable var shadowRadius: CGFloat = 15.0 {
didSet {
self.updateView()
}
}
//Apply params
func updateView() {
self.layer.shadowColor = self.shadowColor.cgColor
self.layer.shadowOpacity = self.shadowOpacity
self.layer.shadowOffset = self.shadowOffset
self.layer.shadowRadius = self.shadowRadius
}
}
Then you can apply this class for you view in the storyboard like:
然后你可以应用这个类在故事板中查看,如:
Now you can edit the values in the attributes inspector:
现在您可以在属性检查器中编辑值:
回答by Chanchal Warde
You can select opacity as you want.
您可以根据需要选择不透明度。
override func awakeFromNib()
{
super.awakeFromNib()
self.viewContainer.layer.shadowOffset = CGSize(width: 0, height: 1) // CGSizeMake(0, 1)
self.viewContainer.layer.shadowColor = UIColor.black.cgColor
self.viewContainer.layer.shadowRadius = 1.5
self.viewContainer.layer.shadowOpacity = 0.65
self.viewContainer.layer.cornerRadius = 1
self.viewContainer.clipsToBounds = true
self.viewContainer.layer.masksToBounds = false
self.layer.masksToBounds = false
}