我正在尝试通过界面生成器向某些 View 添加阴影。我似乎无法让阴影影响我的观点。我查看的所有资源都指向相同的代码,因此我不确定我做错了什么。
界面生成器
界面生成器扩展代码
import Foundation
import UIKit
extension UIView {
//cut irrelevant code for SO Question
@IBInspectable
var masksToBounds: Bool {
get {
return layer.masksToBounds
}
set {
layer.masksToBounds = newValue
}
}
// Shadow handling
@IBInspectable
var shadowColor: UIColor? {
get {
if let color = layer.shadowColor {
return UIColor(cgColor: color)
}
return nil
}
set {
if let color = newValue {
layer.shadowColor = color.cgColor
} else {
layer.shadowColor = nil
}
}
}
@IBInspectable
var shadowOpacity: Float {
get {
return layer.opacity
}
set {
layer.opacity = newValue
}
}
@IBInspectable
var shadowRadius: CGFloat {
get {
return layer.shadowRadius
}
set {
layer.shadowRadius = newValue
}
}
@IBInspectable
var shadowOffset: CGSize {
get {
return layer.shadowOffset
}
set {
layer.shadowOffset = newValue
}
}
}
View 布局
这就是结果
最佳答案
您的 shadowOpacity
属性正在访问错误的图层属性。它正在访问应该是layer.shadowOpacity
的layer.opacity
。另外,您的 shadowRadius
太大了,可能应该在 5 左右(当然,这取决于您的用途),而不是 500。
关于界面生成器中的 iOS 阴影 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50441195/