https://developer.apple.com/documentation/uikit/uiscrollview/keyboarddismissmode/interactive
On a typical UIScrollView
, one may set this property to have the keyboard dismiss interactively alongside a scroll.
Is it possible to achieve this in SwiftUI? If it’s not directly available on ScrollView
, I assume it’s perhaps possible by embedding a UIScrollView
directly. 🤔
Use . immediately to make the keyboard dismiss fully as soon as any scroll happens. Use . interactively to make the keyboard dismiss inline with the user's gesture – they need to scroll further to make it dismiss fully.
Pure SwiftUI (iOS 15) To dismiss the keyboard, simply set view's focusedField to nil . The return key will dismiss keyboard automatically (since iOS 14).
The ScrollView of SwiftUI allows efficient creation of scrolling containers. The view automatically adjust its size to fit the placed objects inside. It will also place some adjustments in order to avoid safe areas. ScrollView can be scrolled horizontally, vertically, or both.
I add the code in my ContentView's onAppear modifier. It can be onDrag
or .interactive
depends on when you want to dismiss keyboard:
struct ContentView: View {
var body: some View {
Text("Hello World")
.onAppear {
UITableView.appearance().keyboardDismissMode = .onDrag
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With