Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created August 7, 2020 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/621b919c89baad7eb95c5249d1a899c2 to your computer and use it in GitHub Desktop.
Save BetterProgramming/621b919c89baad7eb95c5249d1a899c2 to your computer and use it in GitHub Desktop.
// Search view
HStack {
HStack {
//search bar magnifying glass image
Image(systemName: "magnifyingglass").foregroundColor(.secondary)
//search bar text field
TextField("search", text: self.$searchText, onEditingChanged: { isEditing in
self.showCancelButton = true
})
// x Button
Button(action: {
self.searchText = ""
}) {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.secondary)
.opacity(self.searchText == "" ? 0 : 1)
}
}
.padding(8)
.background(Color(.secondarySystemBackground))
.cornerRadius(8)
// Cancel Button
if self.showCancelButton {
Button("Cancel") {
UIApplication.shared.endEditing(true)
self.searchText = ""
self.showCancelButton = false
}
}
}
.padding([.leading, .trailing,.top])
@frugalResolution
Copy link

This searchView is great and works fine. But SwiftUI makes things easy, so you could just add .searchable(text: self.$searchText) below your list to make it native searchable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment