使用 textField.textContentType 时 iOS 键盘闪烁

Ant*_*ony 5 uikeyboard ios swift

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

当 .textContentType 未设置时 当 .textContentType 设置时

我测试了其他代码将光标移动到密码文本字段,它也显示了相同的故障。我认为这与通话有关.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, constant: 10),
            passwordTextField.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -10),
        ])
    }
    
    
    @objc func jumpToolBarButtonTapped() {
        if emailTextField.isFirstResponder == true{
            DispatchQueue.main.async { [self] in
                passwordTextField.becomeFirstResponder()
            }
        } else {
            DispatchQueue.main.async { [self] in
                emailTextField.becomeFirstResponder()
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)