-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added snippet for the searchable modifier
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import SwiftUI | ||
|
||
// During WWDC21, Apple introduced a new modifier .searchable that helps you to show a search bar on your app. | ||
// Here’s how you can add the searchable modifier in your app. | ||
|
||
struct ContentView: View { | ||
|
||
let developers = ["Rod", "Mac", "Stewart", "Lisa", "Andrea", "Steve", "Julia", "Chris", "Penelope"] | ||
|
||
var filteredDevelopers: [String] { | ||
if searchText.isEmpty { | ||
return developers | ||
} else { | ||
return developers.filter { $0.contains(searchText) } | ||
} | ||
} | ||
|
||
@State private var searchText = "" | ||
|
||
var body: some View { | ||
NavigationView { | ||
List { | ||
ForEach(filteredDevelopers, id: \.self) { name in | ||
Text(name) | ||
} | ||
} | ||
.searchable(text: $searchText) | ||
.navigationTitle("My App") | ||
} | ||
} | ||
} |