NavigationSearchBarModifier

main

A clean way to attach a search bar with a scope bar in SwiftUI.
stleamist/NavigationSearchBarModifier

NavigationSearchBarModifier

Build for the latest iOS Build for iOS 14 beta

A clean way to attach a search bar with a scope bar in SwiftUI.

Usage

import SwiftUI
import NavigationSearchBarModifier

struct GroceryList: View {
    
    let groceries: [Grocery]
    @State private var searchControllerIsPresented = false
    @State private var searchTerm: String?
    private var scopes: [String]? = ["Fruit", "Vegetable"]
    @State private var selectedScope: Int = 0
    
    private var predicate: (Grocery) -> Bool {
        if let searchTerm = searchTerm {
            return { grocery in
                grocery.name.localizedCaseInsensitiveContains(searchTerm)
            }
        } else {
            return { _ in true }
        }
    }
    
    var body: some View {
        NavigationView {
            List(groceries.filter(predicate)) { grocery in
                Text(grocery.name)
            }
            .navigationBarTitle("Groceries")
            .navigationSearchBar(
                searchControllerIsPresented: $searchControllerIsPresented,
                searchTerm: $searchTerm,
                searchScopes: scopes,
                selectedSearchScope: $selectedScope,
                hidesWhenScrolling: true
            )
        }
    }
}

Description

  • Swift Tools 5.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Sun Oct 10 2021 16:06:30 GMT-0500 (GMT-05:00)