2021 SwiftUI & UI Frameworks
WWDC21 · 12 min · SwiftUI & UI Frameworks
Craft search experiences in SwiftUI
Discover how you can help people quickly find specific content within your apps. Learn how to use SwiftUI’s .searchable modifier in conjunction with other views to best incorporate search for your app. And we’ll show you how to elevate your implementation by providing search suggestions to help people understand the types of searches they can perform.
Watch at developer.apple.com ↗Code shown on screen · 8 snippets
Colors Suggestions
struct ColorsContentView: View {
var text = ""
var body: some View {
NavigationView {
Sidebar()
DetailView()
}
.searchable(text: $text) {
ForEach(suggestions) { suggestion in
Button {
text = suggestion.text
} label: {
ColorsSuggestionLabel(suggestion)
}
}
}
}
} New Searchable Modifier
ContentView()
.searchable(text: $text) Weather Search
NavigationView {
WeatherList(text: $text) {
ForEach(data) { item in
WeatherCell(item)
}
}
}
.searchable(text: $text) Weather List
struct WeatherList: View {
var text: String
(\.isSearching)
private var isSearching: Bool
var body: some View {
WeatherCitiesList()
.overlay {
if isSearching && !text.isEmpty {
WeatherSearchResults()
}
}
}
} Colors Search
struct ColorsContentView: View {
var text = ""
var body: some View {
NavigationView {
Sidebar()
DetailView()
}
.searchable(text: $text)
}
} Colors Search with TV
struct ColorsContentView: View {
var text = ""
var body: some View {
NavigationView {
#if os(tvOS)
TabView {
Sidebar()
ColorsSearch()
.searchable(text: $text)
}
#else
Sidebar()
DetailView()
#endif
}
#if !os(tvOS)
.searchable(text: $text)
#endif
}
} Colors Search Completions
struct ColorsContentView: View {
var text = ""
var body: some View {
NavigationView {
Sidebar()
DetailView()
}
.searchable(text: $text) {
ForEach(suggestions) { suggestion in
ColorsSuggestionLabel(suggestion)
.searchCompletion(suggestion.text)
}
}
}
} On Submit
ContentView()
.searchable(text: $text) {
MySearchSuggestions()
}
.onSubmit(of: .search) {
fetchResults()
} Related sessions
-
20 min -
40 min