I'm using UIViewRepresentable
to wrap a UITextField
in order to create a TextField
view for SwiftUI which is more fully featured than the currently built-in one. The Coordinator
class is of course the delegate of the UITextField
. The delegate method textField(_:shouldChangeCharactersIn:replacementString:)
is getting called when the user types a character (as it should), but also when an autocorrect suggestion comes up. As far as I know this is not normal UITextField
behavior, and it's very troublesome. Is this because the text field is embedded in a SwiftUI view hierarchy?
SwiftUI - Embedded UITextField invokes the shouldChangeCharacters delegate method for suggested text
Asked
Active
Viewed 225 times
1

jeremyabannister
- 3,796
- 3
- 16
- 25
-
1Please add your code here for us to see. – nanibir Jan 08 '20 at 06:30
-
"when an autocorrect suggestion comes up" – as in, when the user chooses to accept the autocorrect suggestion? Or is this when the user chooses a word from the 3-option suggestions bar above the keyboard? In either of those situations this would be expected behaviour afaik. – Seb Jachec Jan 08 '20 at 10:41
-
What happens is that I type two letters, but the delegate method is run three times. The first time it is run with the first letter I typed “A”, the second time with the two letter word I’ve typed “Ab”, and then a third time with the suggested replacement word “An”. This happens immediately after I type “b” - I do not choose the suggestion, nor implicitly accept it by typing a space, nothing... – jeremyabannister Jan 08 '20 at 16:36