如何在UITextField上显示验证错误?

时间:2018-12-20 15:34:14

标签: ios swift uitextfield

我想在文本字段验证中创建一个与此类似的错误:

enter image description here

我知道在验证错误时可以显示一些本地的iOS红色图标吗?

我如何实现这样的目标?

您知道实现该目标的库或一段代码吗?

2 个答案:

答案 0 :(得分:0)

否,没有任何本地iOS验证错误指示符,您必须使用自己的图像。

因此,您可以将UIImageView和自己的验证错误图像设置为TextField的rightView

yourTextField.rightView = UIImageView(image: UIImage(named: "yourImage"))
yourTextField.rightViewMode = .always

现在根据您的情况,在TableView delegate中的isHidden个方法中rightView中设置func textFieldDidEndEditing(_ textField: UITextField) { // for example when editing did end textField.rightView?.isHidden = isEverythingCorrect }

sonar.jdbc.url=jdbc:sqlserver://server_hostname_or_IP;databaseName=sonar
sonar.jdbc.username=sonarqube
sonar.jdbc.password=mypassword

答案 1 :(得分:0)

1-将rightView分配给UITextField

2-符合UITextFieldDelegate

class ViewController:UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textfield: UITextField!
     override func viewDidLoad() {
        super.viewDidLoad()

        self.textfield.delegate = self
        let img =  UIImageView(image: UIImage(named: "02"))
        img.frame = CGRect(x: 0, y: 0, width: 20, height: 20)
        textfield.rightView = img
        textfield.rightViewMode = .always
    }
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {

        let content = (textField.text! as NSString).replacingCharacters(in: range, with: string)  
        textField.rightView!.isHidden = content.count % 2 == 0 
        return true
    }
}

enter image description here