在 UITextfield 上调用BecomeFirstResponder()时出现此错误 -[RTIInputSystemClient remoteTextInputSessionWithID:performInputOperation:] 执行输入操作需要有效的sessionID。 inputModality = 键盘、inputOperation = 、customInfoType = UIEmojiSearchOperations
当我最初使用此代码创建文本字段时
textField = UITextField(frame: CGRect(origin: .zero, size:frame.size))
textField.text = ""
textField.backgroundColor = UIColor.clear
textField.layer.borderWidth = 0
textField.layer.borderColor = UIColor.clear.cgColor
textField.layer.backgroundColor = UIColor.clear.cgColor
然后设置textField.becomeFirstResponder()就可以了 但是,如果我点击文本字段,我会收到上面的错误,并且当我尝试再次点击文本字段时,我会收到相同的错误。我已经尝试按照类似帖子中的建议关闭自动更正,但这没有任何作用。
我尝试从超级视图中删除文本字段并创建一个全新的文本字段并将其添加到它的位置,但我得到了相同的错误
你能尝试一下这样的代码吗?
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
setupTextField()
}
func setupTextField() {
textField = UITextField(frame: CGRect(origin: .zero, size: CGSize(width: 200, height: 40)))
textField.delegate = self
textField.text = ""
textField.backgroundColor = UIColor.clear
textField.layer.borderWidth = 0
textField.layer.borderColor = UIColor.clear.cgColor
textField.layer.backgroundColor = UIColor.clear.cgColor
self.view.addSubview(textField)
DispatchQueue.main.async {
self.textField.becomeFirstResponder()
}
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
}
override func touchesBegan(_ touches: Set<UITouch>, with event:UIEvent?) {
self.view.endEditing(true) // Dismiss the keyboard when tapping outside the text field
}
}