This question is related to How to hide keyboard when using SwiftUI?
Sample Code:
struct ContentView: View {
@State var text = ""
var body: some View {
Background {
NavigationView {
List {
TextField("TextInput", text: $text)
Text("OnTapGesture")
.onTapGesture {
print("Text Tapped") //works
}
Button("Tap me") {
print("Btn Tapped") //doesn't work
}
NavigationLink("Go to detail", destination: Text("DetailView")) //doesn't work
}
}
}
}
}
struct Background<Content: View>: View {
private var content: Content
init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}
var body: some View {
Rectangle()
.overlay(content)
.onTapGesture {
self.endEditing()
}
}
func endEditing() {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
}
The overlay here works as expected that I can tap anywhere to dismiss the keyboard. However, by adding onTapGesture
on the overlay certain other actions don't work anymore. Is this intended? Is it a bug? Is there a way to work around that?