Skip to content

Instantly share code, notes, and snippets.

@jstheoriginal
Created June 20, 2019 00:42
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jstheoriginal/ebf298b33cdb4a88c3ac5f17f058aa1f to your computer and use it in GitHub Desktop.
Save jstheoriginal/ebf298b33cdb4a88c3ac5f17f058aa1f to your computer and use it in GitHub Desktop.
A simple SwiftUI search bar
struct SearchBar : View {
@Binding var searchText: String
var body: some View {
HStack {
Image(systemName: "magnifyingglass").foregroundColor(.secondary)
TextField(
$searchText,
placeholder: Text("Search")) {
UIApplication.shared.keyWindow?.endEditing(true)
}
Button(action: {
self.searchText = ""
}) {
Image(systemName: "xmark.circle.fill").foregroundColor(.secondary).opacity(searchText == "" ? 0 : 1)
}
}.padding(.horizontal)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment