Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to achieve `ScrollView` interactive keyboard dismissal in SwiftUI?

Tags:

ios

swiftui

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. 🤔

like image 730
Ben Guild Avatar asked Aug 06 '19 20:08

Ben Guild


People also ask

How to dismiss keyboard on scroll in Swift?

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.

How do I dismiss a keyboard in SwiftUI?

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).

What is ScrollView in SwiftUI?

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.


1 Answers

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
            }
    }
}
like image 73
huync Avatar answered Sep 21 '22 07:09

huync