Created
December 30, 2020 02:19
-
-
Save jodyabney/26567f4cdc1d2ac7c3efbf3c68ce5700 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // SearchBar.swift | |
| // MediumArticleFlickrApp | |
| // | |
| // Created by Jody Abney on 12/29/20. | |
| // | |
| import SwiftUI | |
| struct SearchBar: View { | |
| @ObservedObject var viewModel: ViewModel | |
| @State private var isEditing = false | |
| var body: some View { | |
| HStack { | |
| TextField("Search ...", text: $viewModel.searchText) | |
| .padding(7) | |
| .padding(.horizontal, 25) | |
| .background(Color(.systemGray6)) | |
| .cornerRadius(8) | |
| .overlay( | |
| HStack { | |
| Image(systemName: "magnifyingglass") | |
| .foregroundColor(.gray) | |
| .frame(minWidth: 0, maxWidth: .infinity, alignment: .leading) | |
| .padding(.leading, 8) | |
| if isEditing { | |
| Button(action: { | |
| viewModel.searchText = "" | |
| }) { | |
| Image(systemName: "multiply.circle.fill") | |
| .foregroundColor(.gray) | |
| .padding(.trailing, 8) | |
| } | |
| } | |
| } | |
| ) | |
| .padding(.horizontal, 10) | |
| .onTapGesture { | |
| self.isEditing = true | |
| } | |
| if isEditing { | |
| Button(action: { | |
| self.isEditing = false | |
| viewModel.searchText = "" | |
| }) { | |
| Text("Cancel") | |
| } | |
| .padding(.trailing, 10) | |
| .transition(.move(edge: .trailing)) | |
| .animation(.default) | |
| } | |
| } | |
| } | |
| } | |
| struct SearchBar_Previews: PreviewProvider { | |
| static var previews: some View { | |
| SearchBar(viewModel: ViewModel()) | |
| .previewLayout(.sizeThatFits) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment