【发布时间】:2016-11-18 15:44:10
【问题描述】:
我有一个带有自定义代码的 UITextField 来控制角半径和占位符颜色以及其他不同的属性。另外,我有一个带有扩展的协议,可以从任何 UI 元素中投影。
问题是:每当我增加文本字段的圆角半径时,我都会失去阴影。只要圆角半径为0,我还有阴影。
当我增加cornerRadius并失去阴影时,这会在调试器中显示:
setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key height
这是我实施的投影协议:
import UIKit
protocol DropShadow {}
extension DropShadow where Self: UIView {
func addDropShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.7
layer.shadowOffset = CGSize(width: 0, height: 4)
layer.shadowRadius = 3
}
}
这是我的 UITextField 自定义类:
import UIKit
@IBDesignable
class FancyTextField: UITextField, DropShadow {
@IBInspectable var cornerRadius: CGFloat = 0 {
didSet {
layer.cornerRadius = cornerRadius
layer.masksToBounds = cornerRadius > 0
}
}
@IBInspectable var borderWidth: CGFloat = 0 {
didSet {
layer.borderWidth = borderWidth
}
}
@IBInspectable var borderColor: UIColor? {
didSet {
layer.borderColor = borderColor?.cgColor
}
}
@IBInspectable var bgColor: UIColor? {
didSet {
backgroundColor = bgColor
}
}
@IBInspectable var placeHolderColor: UIColor? {
didSet {
let rawString = attributedPlaceholder?.string != nil ? attributedPlaceholder!.string : ""
let str = NSAttributedString(string: rawString, attributes: [NSForegroundColorAttributeName: placeHolderColor!])
attributedPlaceholder = str
}
}
}
【问题讨论】: