如何调用自定义委托函数文本字段采用新大小
How to call CustomDelegate functions Textfield takes it's new size
我想创建一个自定义 UITextfield
但我遇到了这个问题。
我想在 UITextField
下面添加一些标签,问题是当我计算标签的位置时,它不会根据设备大小而是根据 storyboard
大小。
更具体地说,我的代码是
private func setUpBottomRightLabel(){
let height:CGFloat = self.layer.frame.height / 2
let lBottomRight = UILabel(frame: CGRect(x: self.frame.origin.x, y: self.frame.origin.y + self.frame.height + 2, width: self.frame.width * 0.6, height: height))
lBottomRight.font = UIFont(name: "System", size: 4)
lBottomRight.text = "\(self.text?.count ?? 0)/\(self.maxNumber)"
lBottomRight.textAlignment = .right
lBottomRight.backgroundColor = UIColor.red
self.superview!.addSubview(lBottomRight)
self.updateConstraints()
self.layoutIfNeeded()
}
问题是,当从 awakeFromNib()
或 init()
文本字段 self.frame.width = 359
调用此函数时,但当调用 ViewControllers function ViewDidAppear
时 CustomTextField.frame.width is 304.0
.
委托方法我应该从 CustomTextField
文件调用以调用将标签添加到文本字段的方法,该文本字段将采用基于 phone 的大小吗?
在你的CustomTextField
、override
和layoutSubviews
中得到正确的frame
然后添加底部标签
class CustomTextField: UITextField {
override func layoutSubviews() {
print("\(self.frame)")
}
}
我想创建一个自定义 UITextfield
但我遇到了这个问题。
我想在 UITextField
下面添加一些标签,问题是当我计算标签的位置时,它不会根据设备大小而是根据 storyboard
大小。
更具体地说,我的代码是
private func setUpBottomRightLabel(){
let height:CGFloat = self.layer.frame.height / 2
let lBottomRight = UILabel(frame: CGRect(x: self.frame.origin.x, y: self.frame.origin.y + self.frame.height + 2, width: self.frame.width * 0.6, height: height))
lBottomRight.font = UIFont(name: "System", size: 4)
lBottomRight.text = "\(self.text?.count ?? 0)/\(self.maxNumber)"
lBottomRight.textAlignment = .right
lBottomRight.backgroundColor = UIColor.red
self.superview!.addSubview(lBottomRight)
self.updateConstraints()
self.layoutIfNeeded()
}
问题是,当从 awakeFromNib()
或 init()
文本字段 self.frame.width = 359
调用此函数时,但当调用 ViewControllers function ViewDidAppear
时 CustomTextField.frame.width is 304.0
.
委托方法我应该从 CustomTextField
文件调用以调用将标签添加到文本字段的方法,该文本字段将采用基于 phone 的大小吗?
在你的CustomTextField
、override
和layoutSubviews
中得到正确的frame
然后添加底部标签
class CustomTextField: UITextField {
override func layoutSubviews() {
print("\(self.frame)")
}
}