我已经创建了UIView
的@IBDesignable子类
该视图具有几个标签和一个文本字段。
当我尝试在情节提要中更改其值时,不会呈现更改。在cornerRadius不断变化的情况下,storyboard上将显示borderWidth和borderColor。
我想念什么?
在情节提要中的外观如下:
这是它在模拟器中的外观以及我希望它在渲染后在情节提要中的外观:
这是该类的代码:
import UIKit
@IBDesignable
class LoginTextField: UIView {
@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 title: String = "" {
didSet {
titleLabel.text = title
}
}
@IBInspectable var placeholder: String = "" {
didSet {
textField.placeholder = placeholder
}
}
@IBInspectable var message: String = "" {
didSet {
messageLabel.text = message
}
}
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var messageLabel: UILabel!
@IBOutlet var contentView: UIView!
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
override func awakeFromNib() {
super.awakeFromNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
let bundle = Bundle.init(for: type(of: self))
bundle.loadNibNamed("LoginTextField", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}