【问题标题】:Highlight textField in red when user type incorrect input当用户输入不正确的输入时,用红色突出显示 textField
【发布时间】:2017-11-12 09:55:20
【问题描述】:

我想突出显示 textField 边框而不是显示 UIAlertController

这是我的代码,如果你不明白我的问题,请问我

    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var passwordTextField: UITextField!

    override func viewDidLoad() {
            super.viewDidLoad()

           emailTextField.delegate = self
            passwordTextField.delegate = self
    }

    if emailTextField.text != nil && passwordTextField.text != nil {


                activityIndicator.startAnimating()

                Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in
                    if error != nil {
                        AlertController.showAlert(self, titel: "Error", message: "Email or Password incorrect")
                        self.activityIndicator.stopAnimating()
                        return
                    }
                    self.dismiss(animated: true, completion: nil)
                    self.activityIndicator.stopAnimating()
                })

            }
        }

【问题讨论】:

  • 那么,这有什么问题呢?不显示Alert,并添加输入错误的textfield的边框颜色。
  • @mazenqp :使用文本字段委托方法来验证输入并使用这些委托方法更改颜色。
  • 请问我该怎么做

标签: ios swift input error-handling uitextfield


【解决方案1】:

要更改 UITextfield 的边框属性,您需要访问它的图层的边框颜色和边框宽度属性:

emailTextField.layer.borderColor = UIColor.red.cgColor
emailTextField.layer.borderWidth = 1.0

这取决于您,但最好在viewDidLoad 中设置borderWidth,以便在所有状态下保持一致,并且只在回调中调整borderColor。

在您回调的成功部分,您还需要恢复边框颜色:

emailTextField.layer.borderColor = UIColor.black.cgColor

如果passwordTextField 不正确,您可以将emailTextField 替换为passwordTextField。但从您的视图控制器看来,您无法判断错误是来自电子邮件还是密码。使用Error 枚举回传错误类型是个好主意,这样您就可以打开错误并调整 emailTextField 或 passwordTextField 边框颜色。

编辑:

Auth.auth().signIn(withEmail: emailTextField.text!, password: passwordTextField.text!, completion: { (user, error) in
            if error != nil {
                self.emailTextField.layer.borderColor = UIColor.red.cgColor
                self.passwordTextField.layer.borderColor = UIColor.red.cgColor
                self.activityIndicator.stopAnimating()
                return
            }
            self.dismiss(animated: true, completion: nil)
            self.activityIndicator.stopAnimating()
        })

【讨论】:

  • 请把我的代码放在顶部,我知道如何访问那里的属性如果你知道如何在我的代码中显示我需要使用这个属性
  • 我已经更新了尝试答案 - 这是你要找的吗?
  • 我认为这是正确的答案我得到了 n 个错误但是对于我使用的文本字段 ( textFieldEffects ) 它是一个 pod 文件它应该可以工作但我不知道发生了什么没有反应你有什么想法
  • 我以前没有使用过该库,但我假设您使用的是自定义文本字段,它可能会覆盖您对其所做的更改。您可能想要更改边框颜色,但如果您使用的 textfieldeffects 没有边框,那么您将看不到您的更改。我建议阅读他们的文档,看看是否有一个最适合你想要的文本字段效果。如果一切都失败了,那么使用普通的 UITextfield。
【解决方案2】:
class CustomTextField: UITextField {
lazy var bottomBorder: CALayer = {
    let border = CALayer();
    border.borderColor = UIColor.lightGray.cgColor;
    border.borderWidth = 1;
    return border
}()

override func awakeFromNib() {
    super.awakeFromNib()

    borderStyle = .none;
    layer.addSublayer(bottomBorder);
}

override func layoutSubviews() {
    super.layoutSubviews();

    let borderColor = isFirstResponder ? UIColor.yellow: UIColor.lightGray;
    bottomBorder.borderColor = borderColor.cgColor;
    bottomBorder.frame = CGRect(x: 0, y: layer.frame.size.height - 1, width: layer.frame.size.width, height: 1)
}

}

并使用将您的文本字段类设为自定义文本字段

然后像这样使用它

 @IBOutlet weak var emailTextField: CustomTextField!
 emailTextField.bottomBorder.borderColor = UIColor.red.cgColor;

【讨论】:

  • noura 如果你住在 ksa,最好联系因为我需要一些开发人员@Noura
【解决方案3】:

//这里是完美运行的代码

if self.emailTextField.text.isEmpty==true
{
self.emailTextField.layer.borderColor = UIColor.red.cgColor
self.emailTextField.layer.borderWidth = 1.0
}
else
{
 self.emailTextField.layer.borderWidth = 0.0
}
if self.passwordTextField.text.isEmpty==true
{
self.passwordTextField.layer.borderColor = UIColor.red.cgColor
self.passwordTextField.layer.borderWidth = 1.0
}
else
{
self.passwordTextField.layer.borderWidth = 0.0
}

【讨论】:

    猜你喜欢
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-24
    • 2020-01-08
    • 2013-06-04
    相关资源
    最近更新 更多