iOS 15 introduced .searchable()
, which is an official, native,
first-party search bar for SwiftUI. As such this project is no longer necessary.
I will continue to keep it around for projects that need to support older
versions, but it will no longer be actively developed and you should switch to
the new system once you have the choice. Thank you all for your support, and
happy coding!
A small, lightweight UISearchController wrapper for SwiftUI
Update your Package.swift
file:
let package = Package(
...,
dependencies: [
.package(
url: "https://github.com/thislooksfun/SwiftlySearch.git",
from: "1.0.0"),
...
],
...
)
- Go to File > Swift Packages > Add Package Depencency...
- Enter
https://github.com/thislooksfun/SwiftlySearch
as the URL - Select your desired versioning constraint
- Click Next
- Click Finish
import SwiftlySearch
struct MRE: View {
let items: [String]
@State
var searchText = ""
var body: some View {
NavigationView {
List(items.filter { $0.localizedStandardContains(searchText) }) { item in
Text(item)
}.navigationBarSearch(self.$searchText)
}
}
}
(#12) NavigationLink
s inside the resultContent
don't work. This is a limitation of the UIKit/SwiftUI interaction, and thus out of my hands. If you require a seperate view for displaying search results you can use a workaround like shown below:
struct ContentView: View {
@State
var searchText: String = ""
var body: some View {
NavigationView {
ZStack {
if searchText.isEmpty {
NormalView()
} else {
SearchResultsView(text: searchText)
}
}
.navigationBarSearch($searchText)
}
}
}
struct NormalView: View {
var body: some View {
Text("Some view")
}
}
struct SearchResultsView: View {
var text: String
var body: some View {
VStack {
Text("You searched for \(text)")
NavigationLink(destination: Text(text)) {
Text("Let's go!")
}
}
}
}