concept search bar in category ios

appears as: search bar, search bar
iOS Development with Swift

This is an excerpt from Manning's book iOS Development with Swift.

  • Search controllers— You’ll use a search controller to add a search bar to a scene, filtering your data and displaying search results.
  • 10.3.4. Adding a search bar to the collection view

    As you saw when searching the table view controller, you either add the search controller to the navigation bar for iOS 11, or use a search bar as the table header view for prior to iOS 11. Unfortunately, at the time of writing, the iOS 11 technique of adding a search controller to the navigation bar in a collection view doesn’t seem to be working, and collection views don’t have an equivalent to the table header view. There is a workaround, however! Collection views with the Flow layout style do have optional section header views. If you only have one section, this works fine as a header view for the whole collection view that can be used to hold the search bar.

  • Add the following method to your BooksCollectionViewController class:
    // MARK: Header
    override func collectionView(_ collectionView: UICollectionView,
           viewForSupplementaryElementOfKind kind: String,
           at indexPath: IndexPath) -> UICollectionReusableView {
       let reusableView =
           collectionView.dequeueReusableSupplementaryView(ofKind: kind,
           withReuseIdentifier: "collectionHeader", for: indexPath)
       // Customize reusable view here
       return reusableView
    }
    A blank header view should now appear ready to display a search bar.