简体   繁体   English

iOS 16 SwiftUI header 部分中的 TextField(列表)

[英]iOS 16 SwiftUI TextField in section header (List)

If run this code on iOS16 keyboard gets dismissed randomly when character is typed (please see gif), while on iOS15 everything is fine.如果在iOS16上运行此代码,则在键入字符时键盘会随机消失(请参见 gif),而在iOS15上一切正常。

struct ContentView: View {
    
    let names = ["Holly", "Josh", "Rhonda", "Ted"]
    @State var text = ""

    var body: some View {
        List {
            Section {
                ForEach(searchResults, id: \.self) { name in
                    Text(name)
                }
            } header: {
                TextField("Search for name", text: $text)
            }
        }
     
    }
    
    var searchResults: [String] {
        if text.isEmpty {
            return names
        } else {
            return names.filter { $0.contains(text) }
        }
    }
}

It happens when content is in a section with a header. Is it bug from apple introduced in iOS16 or am I doing something wrong?当内容位于 header 的部分时会发生这种情况。它是iOS16中引入的来自苹果的错误还是我做错了什么? Has anyone had the same issue?有没有人有同样的问题?

在此处输入图像描述

It might have something to do with the way List works.这可能与 List 的工作方式有关。 I experimented a bit and if you add .searchable to the Section instead of the List, I am not able to reproduce the problem.我做了一些实验,如果您将.searchable添加到部分而不是列表,我将无法重现该问题。

struct ContentView: View {

    let names = ["Holly", "Josh", "Rhonda", "Ted"]
    @State var text = ""

    var body: some View {
        List {
            Section {
                ForEach(searchResults, id: \.self) { name in
                    Text(name)
                }
            } header: {
                TextField("Search for name", text: $text)
            }.searchable(text: $text) // <- Here
        }
    }

    var searchResults: [String] {
        if text.isEmpty {
            return names
        } else {
            return names.filter { $0.contains(text) }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Searchable adds it's own textfield, you shouldn't add another one, especially not one in a section that is being removed/added. Searchable 添加它自己的文本字段,您不应该添加另一个文本字段,尤其是在正在删除/添加的部分中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM