Swiftpack.co - Package - 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
            )
        }
    }
}

Github

link
Stars: 9

Dependencies

Used By

Total: 0