In Swift, I have added border color red to text fields. I want to implement that if we enter any text in it, the textfield border color should be removed, and if again there is no text in it, the border color should again be set to red. Without a click of any button. (programmatically only)
you can do this with following simple code. add this below line in you viewdidload or view configure method
textField.addTarget(self, action: #selector(ViewController.textFieldDidChange(_:)), for: .editingChanged)
change your text field border color or remove the border as you need
@objc func textFieldDidChange(_ textField: UITextField) {
if (textField.text!.isEmpty)
{
// change your textfield border color
}
else
{
// remove text field border or change color
}
}
This should help:
if self.yourTextField.text.isEmpty
{
self.yourTextField.layer.borderColor = UIColor.red.cgColor
self.yourTextField.layer.borderWidth = 1
}
else
{
self.yourTextField.layer.borderWidth = 0
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.