How set swift 3 UITextField border color?

IosSwiftSwift3Uitextfield

Ios Problem Overview


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 !

Ios Solutions


Solution 1 - Ios

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

}

Solution 2 - Ios

Use the below code in swift 3:

in view did load

 outer_line.layer.borderWidth = 1
 outer_line.layer.borderColor = UIColor.lightGray.cgColor

Solution 3 - Ios

Try to use this, It might be helpful to you

let myColor : UIColor = UIColor( red: 0.5, green: 0.5, blue:0, alpha: 1.0 )
myTextField.layer.masksToBounds = true
myTextField.layer.borderColor = myColor.CGColor
myTextField.layer.borderWidth = 2.0

Solution 4 - Ios

I think you should first provide a borderWidth

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


override func viewDidLoad() {
super.viewDidLoad()

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


}

and then set a color :)

Solution 5 - Ios

> Updated Swift 3 :

if you want to set the bottom border to UITextField, used below lines of code :

// function defination :

func setBottomBorderToTextFields()  {
    
    let bottomLine = CALayer()
    bottomLine.frame = CGRect(x: 0, y: yourTextFieldName.frame.height - 1, width: yourTextFieldName.frame.width, height: 1)
    bottomLine.backgroundColor = UIColor.gray.cgColor // background color
    yourTextFieldName.borderStyle = UITextBorderStyle.none // border style
    yourTextFieldName.layer.addSublayer(bottomLine)
}

// In ViewDidLoad() :

self.setBottomBorderToTextFields()

Solution 6 - Ios

You need to set the borderWidth from the UITextField's layer property.

Like: email.layer.borderWidth = 1.

Also, if you frequently need to set borders to your views, you can make an extension like this:

extension UIView {
    func addBorderAndColor(color: UIColor, width: CGFloat, corner_radius: CGFloat = 0, clipsToBounds: Bool = false) {
        self.layer.borderWidth = width
        self.layer.borderColor = color.cgColor
        self.layer.cornerRadius = corner_radius
        self.clipsToBounds = clipsToBounds
    }
}

Call this like: email.addBorderAndColor(color: UIColor.white, width: 0.5, corner_radius: 5, clipsToBounds: true)

Since this method sets the borderWidth, it will also solve your problem.

Solution 7 - Ios

It is working.

  1. give border width

  2. and after give bgcolor.

txtemail.layer.borderWidth = 1.0
txtemail.layer.borderColor = UIColor.blue.cgColor

It definitely works

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSwiftDeveloperView Question on Stackoverflow
Solution 1 - IosKetan ParmarView Answer on Stackoverflow
Solution 2 - IosNimisha joshyView Answer on Stackoverflow
Solution 3 - IosAshish ThakkarView Answer on Stackoverflow
Solution 4 - IoskamwysocView Answer on Stackoverflow
Solution 5 - IosKiran JadhavView Answer on Stackoverflow
Solution 6 - Iosmeow2xView Answer on Stackoverflow
Solution 7 - Iosuser11803319View Answer on Stackoverflow