我想突出显示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()
})
}
}
答案 0 :(得分:2)
要更改UITextfield边框属性,您需要访问它的图层边框颜色和边框宽度属性:
data-id
这取决于你,但最好在emailTextField.layer.borderColor = UIColor.red.cgColor
emailTextField.layer.borderWidth = 1.0
中设置borderWidth
,以便它在所有状态中保持一致,并且只调整回调中的borderColor。
在回调的成功部分中,您还需要恢复borderColor:
viewDidLoad
如果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()
})
答案 1 :(得分:0)
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)
}
}
并使用make ur textfield class ur CustomTextField
然后像这样使用它
@IBOutlet weak var emailTextField: CustomTextField!
emailTextField.bottomBorder.borderColor = UIColor.red.cgColor;
答案 2 :(得分:0)
//这里的代码可以正常工作
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
}