-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchable.swift
More file actions
31 lines (25 loc) · 882 Bytes
/
Searchable.swift
File metadata and controls
31 lines (25 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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")
}
}
}