Created
July 12, 2020 23:45
-
-
Save jordansinger/46a3fd2090dc6162ceb3dd5f9509e5e9 to your computer and use it in GitHub Desktop.
This file contains 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
import SwiftUI | |
import PlaygroundSupport | |
struct ContentView: View { | |
@State var text = "" | |
var body: some View { | |
SearchBar(text: $text) | |
} | |
} | |
struct SearchBar: UIViewRepresentable { | |
@Binding var text: String | |
class Coordinator: NSObject, UISearchBarDelegate { | |
@Binding var text: String | |
init(text: Binding<String>) { | |
_text = text | |
} | |
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { | |
text = searchText | |
} | |
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { | |
searchBar.resignFirstResponder() | |
} | |
} | |
func makeCoordinator() -> SearchBar.Coordinator { | |
return Coordinator(text: $text) | |
} | |
func makeUIView(context: UIViewRepresentableContext<SearchBar>) -> UISearchBar { | |
let searchBar = UISearchBar(frame: .zero) | |
searchBar.delegate = context.coordinator | |
searchBar.autocapitalizationType = .none | |
searchBar.searchBarStyle = .minimal | |
searchBar.enablesReturnKeyAutomatically = false | |
return searchBar | |
} | |
func updateUIView(_ uiView: UISearchBar, context: UIViewRepresentableContext<SearchBar>) { | |
uiView.text = text | |
uiView.placeholder = "Search" | |
} | |
} | |
PlaygroundPage.current.setLiveView(ContentView()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment