narahari_arjun
narahari_arjun

Reputation: 643

how to set border color for more than one uitextfield on begin editing in swift 3?

I have 3 uitextfields and i have also written the code for setting the border color for uitextfield on begin editing , but when i click on one uitextfield all the other two textfields border also turns to orange.

Need some help

var colorBorder = UIColor(netHex:0xdb8925)
self.uitextfieldAmount.delegate = self
self.uitextfieldBeneficiaryID.delegate = self
self.uitextfieldNote.delegate = self

 func textFieldDidBeginEditing(textField: UITextField) {
    uitextfieldBeneficiaryID.layer.borderColor = colorBorder.CGColor
    //uitextfieldAmount.layer.borderColor = colorBorder.CGColor
    //uitextfieldNote.layer.borderColor = colorBorder.CGColor
}

Upvotes: 0

Views: 988

Answers (3)

RJE
RJE

Reputation: 891

You can use both these delegate methods if you want to highlight the currently editing text field. Please make sure that you have properly set the delegate to each text fields.

func textFieldDidBeginEditing(_ textField: UITextField) {
    textField.layer.borderColor = UIColor.orange.cgColor
}

func textFieldDidEndEditing(_ textField: UITextField) {
    textField.layer.borderColor = UIColor.clear.cgColor
}

Upvotes: 1

Karthick Selvaraj
Karthick Selvaraj

Reputation: 2505

var colorBorder = UIColor(netHex:0xdb8925)
self.uitextfieldAmount.delegate = self
self.uitextfieldBeneficiaryID.delegate = self
self.uitextfieldNote.delegate = self

func textFieldDidBeginEditing(textField: UITextField) {
    if textField == uitextfieldBeneficiaryID {
        uitextfieldBeneficiaryID.layer.borderColor = // Color you want
    } else if textField == uitextfieldAmount {
        uitextfieldAmount.layer.borderColor = // Color you want
    } else {
        uitextfieldNote.layer.borderColor = // Color you want
    }
}

Try like this. Thanks:)

Upvotes: 2

KKRocks
KKRocks

Reputation: 8322

You need to change as below :

func textFieldDidBeginEditing(textField: UITextField) {
    textField.layer.borderColor = colorBorder.CGColor
  }

Upvotes: 0

Related Questions