I want to create a custom UITextfield
and I have this problem.
I want to add some labels below the UITextField
and the problem is that when I compute the position of the label it does't take the position based on the device size but based on the storyboard
size.
To be more spesific my code is
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()
}
And the problem is that when this function is being called from awakeFromNib()
or init()
the textfield self.frame.width = 359
but when the ViewControllers function ViewDidAppear
is being called the CustomTextField.frame.width is 304.0
.
Witch delegate method should I call from CustomTextField
file to call the methods that add the labels to the Textfield that will that will take the size based on the phone?
In your CustomTextField
, override
the layoutSubviews
as below to get the correct frame
and then add the bottom label
class CustomTextField: UITextField {
override func layoutSubviews() {
print("\(self.frame)")
}
}