How set swift 3 UITextField border color?

SwiftDeveloper picture SwiftDeveloper · Jul 19, 2016 · Viewed 79.2k times · Source

Hello i have working no error codes for UITextfield border color change but when using it in Swift 3 dont change textfield border color and dont gives error. I need your help my codes under below.

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


override func viewDidLoad() {
    super.viewDidLoad()

    let myColor : UIColor = UIColor.white()
    email.layer.borderColor = myColor.cgColor
    pass.layer.borderColor = myColor.cgColor


}

Thank you !

Answer

Ketan Parmar picture Ketan Parmar · Jul 19, 2016

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

}