Created
October 23, 2024 16:06
-
-
Save takoikatakotako/0c07ff318f6a36082f351769fc36b92e to your computer and use it in GitHub Desktop.
SwiftUIでSearchBar(TextField)を使って検索する
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 SwiftUI | |
| struct ContentView: View { | |
| let pokemons: [String] = ["Snorlax", "Slowpoke", "Pikachu", "Eevee"] | |
| @State var text: String = "" | |
| var filterdPokemons: [String] { | |
| if text.isEmpty { | |
| return pokemons | |
| } else { | |
| return pokemons.filter {$0.uppercased().contains(text.uppercased())} | |
| } | |
| } | |
| var body: some View { | |
| ScrollView { | |
| LazyVStack { | |
| TextField("Type your search", text: $text) | |
| .padding(8) | |
| .textFieldStyle(RoundedBorderTextFieldStyle()) | |
| ForEach(filterdPokemons, id: \.self) { pokemon in | |
| VStack(alignment: .leading) { | |
| Text(pokemon) | |
| .padding(.leading, 12) | |
| Divider() | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| #Preview { | |
| ContentView() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment