Skip to content

Instantly share code, notes, and snippets.

@prafullakumar
Created March 7, 2021 15:27
Show Gist options
  • Save prafullakumar/3420354db6ed81d7180d8320527f546e to your computer and use it in GitHub Desktop.
Save prafullakumar/3420354db6ed81d7180d8320527f546e to your computer and use it in GitHub Desktop.
struct SearchBar: View {
@Binding var text: String
@Binding var isEditing: Bool
init(text: Binding<String>, isEditing: Binding<Bool>) {
self._text = text
self._isEditing = isEditing
}
var body: some View {
HStack {
TextField("Search Country...", text: $text)
.padding(7.5)
.padding(.horizontal, 20)
.background(Color(.systemGray6))
.cornerRadius(7.5)
.overlay(
HStack {
Image(systemName: "magnifyingglass")
.foregroundColor(.gray)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.padding(.leading, 7.5)
if isEditing && text.count != 0 {
Button(action: {
self.text = ""
}) {
Image(systemName: "multiply.circle.fill")
.foregroundColor(.gray)
.padding(.trailing, 7.5)
}
}
}
)
.padding(.horizontal, 10)
.onTapGesture {
withAnimation {
self.isEditing = true
}
}
if isEditing {
Button(action: {
withAnimation {
self.isEditing = false
self.text = ""
}
// Dismiss the keyboard
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
}) {
Text("Cancel")
}
.padding(.trailing, 10)
.animation(.linear(duration: 0.25))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment