在自定义 class 的 UITextField 上方添加标签
Add a label above UITextField from custom class
我想创建一个自定义 TextField class,我可以在其中添加来自情节提要中的 IBInspectable 属性 的标签(如下所示)。
专注状态
默认状态
我能够在自定义 class 中实现 TextField 的聚焦和非聚焦状态,但无法弄清楚如何将 UILabel 放置在自定义 class 的 TextField 上方。
这是我到目前为止尝试过的方法
@IBDesignable class CustomTextField: UITextField {
override init(frame: CGRect) {
super.init(frame: frame)
setupTextField()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupTextField()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupTextField()
}
private func setupTextField() {
applyDefaultStyle()
addTarget(self, action: #selector(applyActiveStyles), for: UIControl.Event.editingDidBegin)
addTarget(self, action: #selector(applyDefaultStyles), for: UIControl.Event.editingDidEnd)
let label = UILabel()
label.text = "First name"
addSubview(label)
label.bottomAnchor.constraint(equalTo: topAnchor, constant: -10).isActive = true
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0).isActive = true
}
.
.
.
}
但是标签不可见。
向左侧和垂直居中的标签添加约束,不要忘记将 translatesAutoresizingMaskIntoConstraints
设置为 false
private func setupTextField() {
applyDefaultStyle()
addTarget(self, action: #selector(applyActiveStyles), for: UIControl.Event.editingDidBegin)
addTarget(self, action: #selector(applyDefaultStyles), for: UIControl.Event.editingDidEnd)
let label = UILabel()
label.text = "First name"
addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.leftAnchor.constraint(equalTo: leftAnchor, constant: 10).isActive = true
label.bottomAnchor.constraint(equalTo: topAnchor, constant: -2).isActive = true
}
我想创建一个自定义 TextField class,我可以在其中添加来自情节提要中的 IBInspectable 属性 的标签(如下所示)。
专注状态
默认状态
我能够在自定义 class 中实现 TextField 的聚焦和非聚焦状态,但无法弄清楚如何将 UILabel 放置在自定义 class 的 TextField 上方。
这是我到目前为止尝试过的方法
@IBDesignable class CustomTextField: UITextField {
override init(frame: CGRect) {
super.init(frame: frame)
setupTextField()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupTextField()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setupTextField()
}
private func setupTextField() {
applyDefaultStyle()
addTarget(self, action: #selector(applyActiveStyles), for: UIControl.Event.editingDidBegin)
addTarget(self, action: #selector(applyDefaultStyles), for: UIControl.Event.editingDidEnd)
let label = UILabel()
label.text = "First name"
addSubview(label)
label.bottomAnchor.constraint(equalTo: topAnchor, constant: -10).isActive = true
label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 0).isActive = true
}
.
.
.
}
但是标签不可见。
向左侧和垂直居中的标签添加约束,不要忘记将 translatesAutoresizingMaskIntoConstraints
设置为 false
private func setupTextField() {
applyDefaultStyle()
addTarget(self, action: #selector(applyActiveStyles), for: UIControl.Event.editingDidBegin)
addTarget(self, action: #selector(applyDefaultStyles), for: UIControl.Event.editingDidEnd)
let label = UILabel()
label.text = "First name"
addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
label.leftAnchor.constraint(equalTo: leftAnchor, constant: 10).isActive = true
label.bottomAnchor.constraint(equalTo: topAnchor, constant: -2).isActive = true
}