使用 textField.textContentType

问题描述

我发现了一个我无法修复的奇怪小错误。在文本字段之间移动时,工具栏(与文本字段的输入附件视图相关联)和键盘闪烁。它只发生在物理设备上,特别是当文本字段的 textContentType = .password 以及使用代码在文本字段之间移动时,例如在这种情况下,在文本字段之间翻转的“跳转”工具栏按钮。我附上了几个屏幕截图,显示了故障显示内容。它基本上会折叠自动完成密码工具栏一微秒,然后再次显示

When .textContentType is NOT set

When .textContentType is set

我测试了其他代码以将光标移动到密码文本字段,它也显示了相同的故障。我认为这与调用 .becomeFirstResponder 有关,但我不知道如何解决。它不仅发生在 .password.textContentType 选项中,还有其他选项,例如.username.familyName。我需要修复它,因为它不仅会导致轻微的屏幕故障,而且在我的整个项目中,屏幕上还有一堆其他东西会根据键盘的位置移动,而这个故障会触发键盘观察器依次更新键盘框架。

我创建了一个空白项目并且发生了同样的问题。这是代码。如果有人可以提供帮助,将不胜感激。

class ViewController: UIViewController {

    var keyboardToolbar: UIToolbar!
    var emailTextField : UITextField!
    var passwordTextField: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        keyboardToolbar = UIToolbar(frame: CGRect(x: 0,y: 0,width: 100,height: 100))
        let jumpButton = UIBarButtonItem(title: "Jump",style: .plain,target: self,action: #selector(jumpToolBarButtonTapped))
        keyboardToolbar.items = [jumpButton]
        keyboardToolbar.sizetoFit()

        emailTextField = UITextField()
        emailTextField.placeholder = "Email Address TextField"
        emailTextField.keyboardType = .emailAddress
        emailTextField.textContentType = .emailAddress
        emailTextField.autocapitalizationType = .none
        emailTextField.inputAccessoryView = keyboardToolbar
        
        view.addSubview(emailTextField)
        emailTextField.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            emailTextField.topAnchor.constraint(equalTo: view.topAnchor,constant: 100),emailTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor,constant: 10),emailTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor,constant: -10),])
        
        passwordTextField = UITextField()
        passwordTextField.placeholder = "Password TextField"
        passwordTextField.keyboardType = .default
        passwordTextField.autocapitalizationType = .none
        passwordTextField.textContentType = .password
        passwordTextField.inputAccessoryView = keyboardToolbar

        view.addSubview(passwordTextField)
        passwordTextField.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            passwordTextField.topAnchor.constraint(equalTo: emailTextField.bottomAnchor,constant: 20),passwordTextField.leadingAnchor.constraint(equalTo: view.leadingAnchor,passwordTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor,])
    }
    
    
    @objc func jumpToolBarButtonTapped() {
        if emailTextField.isFirstResponder == true{
            dispatchQueue.main.async { [self] in
                passwordTextField.becomeFirstResponder()
            }
        } else {
            dispatchQueue.main.async { [self] in
                emailTextField.becomeFirstResponder()
            }
        }
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)