rnine / NavigationSearchBar

An extension to SwiftUI that will include the UISearchController.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

New in iOS 15

There is now a native way to include a search bar in your SwiftUI apps.

Check out these sources:

  1. Apple Documentation - https://developer.apple.com/documentation/swiftui/form/searchable(text:placement:)
  2. Tutorial - https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-a-search-bar-to-filter-your-data

Prior to iOS 15

NavigationSearchBar

An extension to SwiftUI that will add the UISearchController.

Installation

Manual:

Update your Package.swift file:

let package = Package(
  ...,

  dependencies: [
    .package(
      url: "https://github.com/markvanwijnen/NavigationSearchBar.git",
      from: "1.3.0"),

    ...
  ],

  ...
)

In Xcode:

  1. Go to File > Swift Packages > Add Package Depencency...
  2. Enter https://github.com/markvanwijnen/NavigationSearchBar.git as the URL
  3. Select your desired versioning constraint
  4. Click Next
  5. Click Finish

Usage

import SwiftUI
import NavigationSearchBar

struct ContentView: View {
    @State var text: String = ""
    @State var scopeSelection: Int = 0
    
    var body: some View {
        NavigationView {
            List {
                ForEach(1..<5) { index in
                    Text("Sample Text")
                }
            }
            .navigationTitle("Navigation")
            .navigationSearchBar(text: $text,
                                 scopeSelection: $scopeSelection,
                                 options: [
                                    .automaticallyShowsSearchBar: true,
                                    .obscuresBackgroundDuringPresentation: true,
                                    .hidesNavigationBarDuringPresentation: true,
                                    .hidesSearchBarWhenScrolling: false,
                                    .placeholder: "Search",
                                    .showsBookmarkButton: true,
                                    .scopeButtonTitles: ["All", "Missed", "Other"]
                                 ],
                                 actions: [
                                    .onCancelButtonClicked: {
                                        print("Cancel")
                                    },
                                    .onSearchButtonClicked: {
                                        print("Search")
                                    },
                                    .onBookmarkButtonClicked: {
                                        print("Present Bookmarks")
                                    }
                                 ], searchResultsContent: {
                                     NavigationLink(destination: Text("Destination")) {
                                         Text("Search Results for \(text) in \(String(scopeSelection))")
                                     }
                                 })
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Donate

If you have been enjoying my free Swift package, please consider showing your support by buying me a coffee through the link below. Thanks in advance!

Buy Me A Coffee

About

An extension to SwiftUI that will include the UISearchController.

License:MIT License


Languages

Language:Swift 100.0%