【问题标题】:how to set border color for more than one uitextfield on begin editing in swift 3?如何在swift 3中开始编辑时为多个uitextfield设置边框颜色?
【发布时间】:2016-11-15 04:41:34
【问题描述】:

我有 3 个 uitextfields,并且我还编写了用于在开始编辑时为 uitextfield 设置边框颜色的代码,但是当我单击一个 uitextfield 时,所有其他两个文本字段的边框也会变成橙色。

需要帮助

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
}

【问题讨论】:

  • 然后使用条件。
  • 只需使用func textFieldDidBeginEditing(textField: UITextField) { textField.layer.borderColor = colorBorder.CGColor }
  • 感谢您的时间@Anbu.Karthik,我是 swift ios 的新手,如果像您这样的人能指导我提高我的 swift 技能,那就太好了。
  • 不像那个兄弟,我不是google的天才,同时每个人都不是什么都知道,每天练习它会自动提高自己,所以现在不要感觉。

标签: ios swift


【解决方案1】:
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
    }
}

试试这样。谢谢:)

【讨论】:

    【解决方案2】:

    如果要突出显示当前正在编辑的文本字段,可以使用这两种委托方法。 请确保您已将委托正确设置到每个文本字段。

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

    【讨论】:

      【解决方案3】:

      您需要进行如下更改:

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

      【讨论】:

      • 感谢您的时间@KKRocks
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      相关资源
      最近更新 更多