Change the border color of uitextfield

2019-09-16 15:28发布

问题:

I added bottom border to UITextField this way:

let bottomLine = CALayer()
bottomLine.frame = CGRectMake(0.0, self.frame.height, self.frame.width, CGFloat(borderWidth))
bottomLine.backgroundColor = borderColor.CGColor
self.borderStyle = UITextBorderStyle.None
self.layer.addSublayer(bottomLine)

How can I change the border color of this bottom line when UITextField call textFieldDidBeginEditing() and then change back color to default when it calls textFieldDidEndEditing()

I'm using a custom class of UITextField

回答1:

Take Action methods of textfield as editing did begin and editing did end

This will work fine, I have tested it

   @IBAction func didbegin(_ sender: AnyObject) {
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.green.cgColor
        border.frame = CGRect(x: 0, y: txtfield.frame.size.height - width, width:  txtfield.frame.size.width, height: txtfield.frame.size.height)

        border.borderWidth = width
        txtfield.layer.addSublayer(border)
        txtfield.layer.masksToBounds = true
    }


    @IBAction func endediting(_ sender: AnyObject) {
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.black.cgColor
        border.frame = CGRect(x: 0, y: txtfield.frame.size.height - width, width:  txtfield.frame.size.width, height: txtfield.frame.size.height)

        border.borderWidth = width
        txtfield.layer.addSublayer(border)
        txtfield.layer.masksToBounds = true

    }

Hope this thing helps you.



回答2:

Try just re-adding the layer with the default color in the textFieldDidEndEditing method, like so:

func textFieldDidBeginEditing(_ textField: UITextField) { 
        let bottomLine = CALayer()
         bottomLine.frame = CGRectMake(0.0, self.frame.height, self.frame.width, CGFloat(borderWidth))
        bottomLine.backgroundColor = borderColor.CGColor
        self.borderStyle = UITextBorderStyle.None
        self.layer.addSublayer(bottomLine)
    }

 func textFieldDidEndEditing(_ textField: UITextField) {
    let bottomLine = CALayer()
    bottomLine.frame = CGRectMake(0.0, self.frame.height, self.frame.width, CGFloat(borderWidth))
    bottomLine.backgroundColor = defaultColor.CGColor
    self.borderStyle = UITextBorderStyle.None
    self.layer.addSublayer(bottomLine)
}

This worked for me



回答3:

    func viewWillAppear(_ animated: Bool) {
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.white.cgColor
        border.frame = CGRect(x: 0, y: yourTextFeild.frame.size.height - width, width:  yourTextFeild.frame.size.width, height: yourTextFeild.frame.size.height)

        border.borderWidth = width
        yourTextFeild.layer.addSublayer(border)
        yourTextFeild.layer.masksToBounds = true
    }   

 //Mark Textfield Delegate
    func textFieldDidBeginEditing(_ textField: UITextField) {

        for layer in  textField.layer.sublayers!{
            layer.borderColor = UIColor.green.cgColor

      }
   }

    func textFieldDidEndEditing(_ textField: UITextField) {
        for layer in  textField.layer.sublayers!{
            layer.borderColor = UIColor.white.cgColor   
        }
    }