8000 GitHub - Geri-Borbas/iOS.Blog.SwiftUI_Search_Bar_in_Navigation_Bar: ๐Ÿ” SwiftUI (1) search bar in the navigation bar.
[go: up one dir, main page]

Skip to content

Geri-Borbas/iOS.Blog.SwiftUI_Search_Bar_in_Navigation_Bar

Repository files navigation

SwiftUI Search Bar in Navigation Bar

๐Ÿ” SwiftUI (1) search bar in the navigation bar.

Complementary repository for article SwiftUI Search Bar in the Navigation Bar. For more details on motivations and implementation please refer to the full article, or lookup the basic usage example below otherwise.

struct ContentView: View
{
    
    var planets =
        ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"] +
        ["Ceres", "Pluto", "Haumea", "Makemake", "Eris"]
    
    @ObservedObject var searchBar: SearchBar = SearchBar()
    
    var body: some View {
        NavigationView {
            List {                
                ForEach(
                    planets.filter {
                        searchBar.text.isEmpty ||
                        $0.localizedStandardContains(searchBar.text)
                    },
                    id: \.self
                ) { eachPlanet in
                    Text(eachPlanet)
                }
            }
                .navigationBarTitle("Planets")
                .add(self.searchBar)
        }
    }
}

License

Licensed under the MIT License.

0