Uitextfield Border Color

How to change uitextfield border when selected

If you want to highlight TextField when user starts typing, I suggest you to use UITextField delegate method textFieldDidBeginEditing for handling when user starts typing.

func textFieldDidBeginEditing(_ textField: UITextField) {
if textField == email {
textField.layer.borderWidth = 4
textField.layer.borderColor = // your color
} else if ...
}

you can also use textFieldDidEndEditing delegate method for handling when user is done with typing

func textFieldDidEndEditing(_ textField: UITextField) {
textField.layer.borderWidth = // default width
textField.layer.borderColor = // default color
}

So if you want to use these methods, don't forget to set delegate properties of your TextFields in viewDidLoad for example

email.delegate = self
password.delegate = self
userName.delegate = self

also don't forget to implement UITextFieldDelegate protocol

class ViewController: UIViewController, UITextFieldDelegate

How set swift 3 UITextField border color?

You also need to set border width, because your border color is set already but your default border width is 0.0 so you can't see it.

So, set border width something like,

  email.layer.borderWidth = 1.0

Update :

Your code should be like,

 @IBOutlet weak var email: UITextField!
@IBOutlet weak var pass: UITextField!

override func viewDidLoad() {
super.viewDidLoad()

let myColor = UIColor.white
email.layer.borderColor = myColor.cgColor
pass.layer.borderColor = myColor.cgColor

email.layer.borderWidth = 1.0
pass.layer.borderWidth = 1.0

}


Related Topics



Leave a reply



Submit