本文介绍了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)")
                    }
                }
            }
        }
    }
}



滚动超过 2 个单元格/几个点时如何关闭键盘?



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

推荐答案

如果您使用的是 ScrollView(可能也使用 List 但我还没有确认),你可以使用UIScrollView appearance,不过这会影响所有的ScrollViews.

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

这篇关于SwiftUI - 滚动时关闭键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 20:34