微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SearchBar列表条目不会被取消选择

如何解决SearchBar列表条目不会被取消选择

我使用了Antoine Weber的this代码通过SwiftUI创建了一个SearchBar。在iOS 13上运行良好,但在iOS 14 Beta中,按下列表后不会取消选择列表条目。 我附上一张照片,向您展示问题。

我使用了以下代码

extension UIApplication {
    func endEditing(_ force: Bool) {
        self.windows
            .filter{$0.isKeyWindow}
            .first?
            .endEditing(force)
    }
}

struct ResignKeyboardOnDragGesture: ViewModifier {
    var gesture = DragGesture().onChanged{_ in
        UIApplication.shared.endEditing(true)
    }
    func body(content: Content) -> some View {
        content.gesture(gesture)
    }
}

extension View {
    func resignKeyboardOnDragGesture() -> some View {
        modifier(ResignKeyboardOnDragGesture())
    }
}


struct SearchBarView: View {
    
    @Binding var searchText: String
    @State private var showCancelButton: Bool = false
    var onCommit: () ->Void = {print("onCommit")}
    
    var body: some View {
        HStack {
            HStack {
                Image(systemName: "magnifyingglass")
                
                // Search text field
                ZStack (alignment: .leading) {
                    if searchText.isEmpty { // Separate text for placeholder to give it the proper color
                        Text("Search")
                    }
                    TextField("",text: $searchText,onEditingChanged: { isEditing in
                        self.showCancelButton = true
                    },onCommit: onCommit).foregroundColor(.primary)
                }
                // Clear button
                Button(action: {
                    self.searchText = ""
                }) {
                    Image(systemName: "xmark.circle.fill").opacity(searchText == "" ? 0 : 1)
                }
            }
            .padding(EdgeInsets(top: 8,leading: 6,bottom: 8,trailing: 6))
            .foregroundColor(.secondary) // For magnifying glass and placeholder test
            .background(Color(.tertiarySystemFill))
            .cornerRadius(10.0)
            
            if showCancelButton  {
                // Cancel button
                Button("Cancel") {
                    UIApplication.shared.endEditing(true) // this must be placed before the other commands here
                    self.searchText = ""
                    self.showCancelButton = false
                }
                .foregroundColor(Color(.systemBlue))
            }
        }
        .padding(.horizontal)
        .navigationBarHidden(showCancelButton)
    }
}

#if DEBUG
struct SearchBarView_Previews: PreviewProvider {

    static var previews: some View {
        SearchBarView(searchText: .constant(""))

    }
}
#endif

我的ContentView具有以下代码

struct ContentView: View {
    
    let array = ["First","Second","Third","Fourth"]
    let secondarray = ["1","2","3","4"]
    @State private var searchText = ""
    
    var body: some View {
        NavigationView() {
            vstack {
                SearchBarView(searchText: $searchText)
                List {
                    
                    ForEach(secondarray,id: \.self) { section in
                        Section(header: Text(section)) {
                            ForEach(array.filter {
                                self.searchText.isEmpty ? true
                                    : $0.contains(self.searchText)
                            },id: \.self) { index in
                                    ListRowView(text: index)
                            }
                        }
                    }
                }
                .resignKeyboardOnDragGesture()
                .navigationBarTitle("Test")
            }
            
        }
    }
}

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

Screenshot simulator Xcode 12 Beta 5 with iPhone 11 Pro

感谢您的帮助!

致谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。