Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI - Close Keyboard on Scroll

Tags:

ios

swiftui

I have a simple search list:


struct ContentView: View {
    @State var text:String = ""
    var items = 1...100
    var body: some View {
        VStack {
            List {
                TextField("Search", text: $text)
                Section{
                    ForEach(items.filter({"\($0)".contains(text)}),id: \.self){(i) in
                       Text("option \(i)")
                    }
                }
            }
        }
    }
}

iOS simulator screenshot

How can I make the keyboard close when scrolling for more than 2 cells/few points?

like image 346
Daniel Meltzer Avatar asked Oct 24 '19 11:10

Daniel Meltzer


1 Answers

If you are using a ScrollView (probably also with a List but I haven't confirmed it), you could use the UIScrollView appearance, this will affect all ScrollViews though.

UIScrollView.appearance().keyboardDismissMode = .onDrag
like image 75
vauxhall Avatar answered Sep 27 '22 15:09

vauxhall