当用户键入不正确的输入时,以红色突出显示 textField

Highlight textField in red when user type incorrect input

我想突出显示 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()
                })

            }
        }

为了更改 UITextfield 边框 属性 您需要访问其层的边框颜色和边框宽度属性:

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

这取决于你,但最好在 viewDidLoad 中设置 borderWidth,这样它在所有状态下都是一致的,并且只在回调中调整 borderColor。

在回调的成功部分,您还需要恢复 borderColor:

emailTextField.layer.borderColor = UIColor.black.cgColor

如果 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()
        })
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)
}

}

并使用使你的文本字段 class 你的 CustomTextField

然后像这样使用它

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

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

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
}