Skip to content

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

Notifications You must be signed in to change notification settings

stleamist/NavigationSearchBarModifier

Repository files navigation

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
            )
        }
    }
}

About

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

Topics

Resources

Stars

Watchers

Forks

Languages