简体   繁体   中英

change bottom line border of uitextfield swift

I'm trying to change border properties with the following code:

class SimpleTextField: UITextField, UITextFieldDelegate {

      override func layoutSubviews() {
        super.layoutSubviews()

        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.black.cgColor
        border.frame = CGRect(x: 0, y: self.frame.size.height - width, width:  self.frame.size.width, height: self.frame.size.height)

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

       self.delegate = self


    }

 func textFieldDidBeginEditing(_ textField: UITextField) {
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.green.cgColor
        border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width:  textField.frame.size.width, height: textField.frame.size.height)

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

    func textFieldDidEndEditing(_ textField: UITextField) {
        let border = CALayer()
        let width = CGFloat(2.0)
        border.borderColor = UIColor.black.cgColor
        border.frame = CGRect(x: 0, y: textField.frame.size.height - width, width:  textField.frame.size.width, height: textField.frame.size.height)

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

I wrote code in layoutSubviews() function as a default appearance for textField . I used textFieldDidBeginEditing() and textFieldDidEndEditing() functions for changing bottom border color. If I use the code layoutSubviews() the border color won't be changed at all but if I remove the code that I use as default appearance, border color will change but I don't have default border as the first time when text field is appeared.

Don't create new layer every time you start/end editing the text field. Instead, reuse it: make it a property and then just update when needed accordingly.

The default appearance you can set at the same point where the text field itself and the border are created, eg:

let textField = UITextField(frame: CGRect(x: 100.0, y: 100.0, width: 200.0, height: 50.0))
let border = CALayer()

override func viewDidLoad() {
    super.viewDidLoad()

    view.addSubview(textField)
    textField.layer.addSublayer(border)
    textField.delegate = self

    updateBorder(textField: textField, color: UIColor.black, width: 2.0)
}

func updateBorder(textField: UITextField, color: UIColor, width: CGFloat) -> Void {
    border.backgroundColor = color.cgColor
    border.frame = CGRect(x: 0.0, y: textField.frame.size.height - width, width: textField.frame.size.width, height: width);
}

func textFieldDidBeginEditing(_ textField: UITextField) {
    updateBorder(textField: textField, color: UIColor.green, width: 2.0)
}

func textFieldDidEndEditing(_ textField: UITextField) {
    updateBorder(textField: textField, color: UIColor.black, width: 2.0)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM