I know there are answers for dismissing keyboard, but mostly they are triggered on tapped outside of the keyboard. As I stated in the question, how to achieve dismissing keyboard on swipe (to bottom).
3 Answers
UIScrollView has keyboardDismissMode
which when set to interactive
, will achieve what you want. SwiftUI doesn’t provide direct support for this, but since under the hood, SwiftUI is using UIScrollView, you can use this which sets keyboardDismissMode to interactive for all scroll views in your app.
UIScrollView.appearance().keyboardDismissMode = .interactive
You must have a ScrollView in your view hierarchy for this to work. Here’s is a simple view demonstrating the behavior:
struct ContentView: View {
@State private var text = "Hello, world!"
var body: some View {
ScrollView {
TextField("Hello", text: $text)
.padding()
}
.onAppear {
UIScrollView.appearance().keyboardDismissMode = .interactive
}
}
}
The only caveat is that this affects all scroll views in your app. I don’t know of a simple solution if you only want to affect one scroll view in your app.

- 4,889
- 3
- 25
- 17
-
This answer seems does the job thanks, I cannot upvote, since my reputation is not enough, but you can upvote my question :) – Ercan Baba Aug 28 '20 at 14:21
-
1Mark, I found that using that would cause the emoji keyboard to weirdly shrink to your finger tip when trying to horizontally scroll through the emojis - do you know if there's anyway to fix that? Thanks! – hyouuu Aug 31 '21 at 05:11
For example if you have a list of messages then you can :
List {
ForEach(...) { ...
}
}.resignKeyboardOnDragGesture()
extension View {
func resignKeyboardOnDragGesture() -> some View {
return modifier(ResignKeyboardOnDragGesture())
}
}
struct ResignKeyboardOnDragGesture: ViewModifier {
var gesture = DragGesture().onChanged { _ in
UIApplication.shared.endEditing(true)
}
func body(content: Content) -> some View {
content.gesture(gesture)
}
}
By the way it came from here : https://stackoverflow.com/a/58564739/7974174

- 498
- 8
- 23
You can add
.simultaneousGesture(
// Hide the keyboard on scroll
DragGesture().onChanged { _ in
UIApplication.shared.sendAction(
#selector(UIResponder.resignFirstResponder),
to: nil,
from: nil,
for: nil
)
}
)
to the view.

- 1,684
- 1
- 15
- 27