I have a use case where I call insertText(_ text: String) on UITextField and expect the iOS Keyboard's return key (Go button in my case) to get enabled. It was disabled before inputing the text since there was no text in the UITextField. This was working until iOS 11.x but stopped working with iOS 12.
Consider the use case below, here on screen there is a UITextField and a button which is inserting the text:
class ViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.textField.enablesReturnKeyAutomatically = true
self.textField.becomeFirstResponder()
}
//pressing the button inserts the text in the textfield whiich is expected to enable the `Go` button
@IBAction func TestPressed(_ sender: Any) {
self.textField.insertText("abc")
//refreshes the keyboard to enable the Go button but does not work on iOS 12
self.textField.becomeFirstResponder()
//calling
}
}
Also note that if we replace insertText call above with self.textField.text = "abc", it does enable the Go button as expected.
There are two ways to solve this:
@IBAction func TestPressed(_ sender: Any) {
self.textField.insertText("abc")
//refreshes the keyboard (works on iOS 12)
self.textField.resignFirstResponser()
self.textField.becomeFirstResponder()
}
or
@IBAction func TestPressed(_ sender: Any) {
self.textField.insertText("abc")
//refreshes the keyboard (works on iOS 12)
self.textField.reloadInputViews()
}
Wanted to know what is the right way to do and if this is actually a bug in iOS 12 ?
From the documentation it seems reloadInputViews() should be the right way to refresh the keyboard in all cases.