Post

Replies

Boosts

Views

Activity

TextField Binding and auto correct in SwiftUI
Hi, I've noticed an interesting behaviour with TextField bindings in SwiftUI where it appears any auto correct updates don't trigger an update on the binding which can lead to an inconsistent state. As an example: struct ContentView: View {     @State var itemName = ""     @State var itemDescription = ""     private var summary: String {         [             itemName,             itemDescription         ]         .filter { !$0.isEmpty }         .joined(separator: " - ")     }     var body: some View {         VStack(alignment: .leading) {             HStack {                 Text("Name:")                 TextField("Item Name", text: $itemName)             }             HStack {                 Text("Description:")                 TextField("Item Description", text: $itemDescription)             }             VStack {                 Text("Summary:")                 Text(summary)                     .foregroundColor(.gray)             }         }.padding()     } } Typing a name that is misspelt (e.g. "Pper") and then taping on the description text field will trigger an auto correct update (e.g. to "Paper"), however the itemName property never gets updated and remains with the previous misspelt version. I see TextField has a few additional properties that can be used, onEditingChanged: (Bool) -> Void and onCommit: () -> Void which do get triggered once the text field loses focus, however don't have a reference to the final text value of the text field. Does anyone have any suggestions / advice on how to deal with this scenario to ensure the state is always in sync? Thanks!
0
0
778
Oct ’20