如何为具有圆角的 UIView 添加阴影
How to add shadow to UIView that has round corners
我知道这个问题已经被问过,但我尝试了必须的答案,但它似乎对我不起作用。
所以我想要一个 UIView,它在视图的两侧和按钮周围有阴影,而不是在顶部。我该怎么做,因为我有圆角。
这是 UI 的样子:
这是我到目前为止尝试过的方法(似乎不起作用):
featureOneView.layer.cornerRadius = 10.0
featureOneView.clipsToBounds = true
featureOneView.layer.shadowOffset = CGSize(width: 0, height: 3)
featureOneView.layer.shadowOpacity = 0.6
featureOneView.layer.shadowRadius = 3.0
featureOneView.layer.shadowColor = UIColor.gray.cgColor
我相信问题出在限制范围上。如果你想让视图裁剪边界,你应该有两个视图 - 一个用于阴影,一个用于内容。
这对你有用吗?
// corner radius
featureOneView.layer.cornerRadius = 10
// shadow
featureOneView.layer.shadowColor = UIColor.black.cgColor
featureOneView.layer.shadowOffset = CGSize(width: 3, height: 3)
featureOneView.layer.shadowOpacity = 0.6
featureOneView.layer.shadowRadius = 3.0
提供了快速 google 搜索:
我写了一个 IBDesingable class,效果很好
@IBDesignable
class RoundedButton: UIButton {
@IBInspectable var cornerRadius: CGFloat = 10.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
@IBInspectable var borderWidth: CGFloat = 0.0 {
didSet {
self.layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
@IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}
@IBInspectable
var shadowOpacity: Float {
get {
return layer.shadowOpacity
}
set {
layer.shadowOpacity = newValue
}
}
@IBInspectable
var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}
@IBInspectable
var shadowColor: CGColor? {
get {
if let color = layer.shadowColor {
return color
}
return nil
}
set {
if let color = newValue {
layer.shadowColor = color
} else {
layer.shadowColor = nil
}
}
}
override func awakeFromNib() {
self.setupView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
}
func setupView() {
self.layer.cornerRadius = cornerRadius
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor.cgColor
self.layer.shadowColor = shadowColor ?? UIColor.gray.cgColor
self.layer.shadowOffset = shadowOffset
self.layer.shadowRadius = shadowRadius
self.layer.shadowOpacity = shadowOpacity
}
}
您可以添加框架/圆角半径/偏移量/颜色/透明度
希望这有帮助
我知道这个问题已经被问过,但我尝试了必须的答案,但它似乎对我不起作用。
所以我想要一个 UIView,它在视图的两侧和按钮周围有阴影,而不是在顶部。我该怎么做,因为我有圆角。
这是 UI 的样子:
这是我到目前为止尝试过的方法(似乎不起作用):
featureOneView.layer.cornerRadius = 10.0
featureOneView.clipsToBounds = true
featureOneView.layer.shadowOffset = CGSize(width: 0, height: 3)
featureOneView.layer.shadowOpacity = 0.6
featureOneView.layer.shadowRadius = 3.0
featureOneView.layer.shadowColor = UIColor.gray.cgColor
我相信问题出在限制范围上。如果你想让视图裁剪边界,你应该有两个视图 - 一个用于阴影,一个用于内容。
这对你有用吗?
// corner radius
featureOneView.layer.cornerRadius = 10
// shadow
featureOneView.layer.shadowColor = UIColor.black.cgColor
featureOneView.layer.shadowOffset = CGSize(width: 3, height: 3)
featureOneView.layer.shadowOpacity = 0.6
featureOneView.layer.shadowRadius = 3.0
提供了快速 google 搜索:
我写了一个 IBDesingable class,效果很好
@IBDesignable
class RoundedButton: UIButton {
@IBInspectable var cornerRadius: CGFloat = 10.0 {
didSet {
self.layer.cornerRadius = cornerRadius
}
}
@IBInspectable var borderWidth: CGFloat = 0.0 {
didSet {
self.layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) {
didSet {
self.layer.borderColor = borderColor.cgColor
}
}
@IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}
@IBInspectable
var shadowOpacity: Float {
get {
return layer.shadowOpacity
}
set {
layer.shadowOpacity = newValue
}
}
@IBInspectable
var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}
@IBInspectable
var shadowColor: CGColor? {
get {
if let color = layer.shadowColor {
return color
}
return nil
}
set {
if let color = newValue {
layer.shadowColor = color
} else {
layer.shadowColor = nil
}
}
}
override func awakeFromNib() {
self.setupView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
}
func setupView() {
self.layer.cornerRadius = cornerRadius
self.layer.borderWidth = borderWidth
self.layer.borderColor = borderColor.cgColor
self.layer.shadowColor = shadowColor ?? UIColor.gray.cgColor
self.layer.shadowOffset = shadowOffset
self.layer.shadowRadius = shadowRadius
self.layer.shadowOpacity = shadowOpacity
}
}
您可以添加框架/圆角半径/偏移量/颜色/透明度 希望这有帮助