Target iOS 13
From the SwiftUI TextField listening events from the return button just use the onEditingChanged
parameter of TextField
This is Specially used when someone using TextField For SearchBar in SwiftUI finding solution similar to searchBarSearchButtonClicked where the keyboard dismissed as well.
struct ContentView: View {
@State var searchText = ""
var body: some View {
TextField("Search Bar SwiftUI", text: $searchText, onEditingChanged: { changed in
if changed {
}
else {
print("User clicked on Return Key")
}
})
}
}
Solution for iOS 15 onwards :
struct ContentView: View {
@State var searchText = ""
var body: some View {
TextField("Search Bar SwiftUI", text: $searchText)
.onSubmit {
print("User clicked on return key")
}
}
}