Change the border color of uitextfield

喜夏-厌秋 提交于 2019-12-12 04:23:13

问题


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   
        }
    }


来源:https://stackoverflow.com/questions/41453124/change-the-border-color-of-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!