SwiftUI Tap Anywhere
Today I learned how to implement tap anywhere to dismiss the keyboard on SwiftUI.
In your parent stack, either VStack
or HStack
, add the following code.
.contentShape(Rectangle())
.onTapGesture {
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}
The most important thing to take note here is the contentShape modifier or tap gesture will not work.