【问题标题】:change bottom line border of uitextfield swift快速更改uitextfield的底线边框
【发布时间】:2017-01-04 22:11:36
【问题描述】:

我正在尝试使用以下代码更改边框属性:

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

我在layoutSubviews() 函数中编写了代码作为textField 的默认外观。我使用textFieldDidBeginEditing()textFieldDidEndEditing() 函数来更改底部边框颜色。如果我使用代码layoutSubviews(),边框颜色根本不会改变,但如果我删除我用作默认外观的代码,边框颜色会改变,但我第一次没有默认边框作为文本字段出现了。

【问题讨论】:

    标签: ios swift uitextfield


    【解决方案1】:

    不要在每次开始/结束编辑文本字段时创建新图层。相反,重用它:使其成为一个属性,然后在需要时进行相应的更新。

    您可以在创建文本字段本身和边框的同一点设置默认外观,例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-25
      • 2012-06-04
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多