Last active
March 15, 2021 13:23
-
-
Save gtokman/36450a6fc346b549f1fb218f9db43c3e to your computer and use it in GitHub Desktop.
SwiftUIX simple search bar - https://github.com/SwiftUIX/SwiftUIX.git
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
| import SwiftUIX | |
| struct Friend: Identifiable { // Example model | |
| let id: UUID = .init() | |
| let name: String | |
| } | |
| struct ContentView: View { | |
| @State var friends: [Friend] = [ | |
| .init(name: "Gary"), | |
| .init(name: "Myles"), | |
| .init(name: "Chris"), | |
| .init(name: "Scott"), | |
| .init(name: "Gary"), | |
| .init(name: "Liam"), | |
| ] | |
| @State var searchText = "" | |
| @State var isEditing = false | |
| var body: some View { | |
| NavigationView { | |
| List(friends) { friend in | |
| HStack { | |
| Text(friend.name) | |
| Spacer() | |
| } | |
| .contentShape(Rectangle()) // Needed to select cell | |
| .onTapGesture { | |
| // print(friend.name) | |
| // Do something on selection | |
| } | |
| } | |
| .navigationTitle("Friends") | |
| .navigationSearchBar { // 1 | |
| SearchBar("Search Friends", | |
| text: $searchText, | |
| isEditing: $isEditing) { // 2 | |
| // Do something on enter // 3 | |
| } | |
| .onCancel { | |
| // Do something on cancel // 4 | |
| } | |
| .searchBarStyle(.default) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment