如何解决使用 textField.textContentType
我发现了一个我无法修复的奇怪小错误。在文本字段之间移动时,工具栏(与文本字段的输入附件视图相关联)和键盘闪烁。它只发生在物理设备上,特别是当文本字段的 textContentType = .password
以及使用代码在文本字段之间移动时,例如在这种情况下,在文本字段之间翻转的“跳转”工具栏按钮。我附上了几个屏幕截图,显示了故障显示的内容。它基本上会折叠自动完成密码工具栏一微秒,然后再次显示:
我测试了其他代码以将光标移动到密码文本字段,它也显示了相同的故障。我认为这与调用 .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 举报,一经查实,本站将立刻删除。