Skip to content

Instantly share code, notes, and snippets.

@wb-towa
Forked from jstheoriginal/SearchBar.swift
Created December 30, 2020 17:43
Show Gist options
  • Save wb-towa/ae9cd3acc50b139fc21000add28d2571 to your computer and use it in GitHub Desktop.
Save wb-towa/ae9cd3acc50b139fc21000add28d2571 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