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

时间:2020-08-22 16:32:05

标签: ios xcode swiftui swift5

我使用了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

感谢您的帮助!

致谢

0 个答案:

没有答案