Search code examples
swiftuiviewcontrolleruisearchbarapple-tvuiviewcontrollerrepresentable

Apple TV: Search bar with keyboard and filters


I'm implementing a Search feature for my app for AppleTV. I was wondering if anyone knows how to implement for apple tv this search bar with keyboard + filtering (as screenshot bellow). I found that the Music App for apple tv enable us to filter the search by "All Apple Music" and "Library" I'm not finding any reference and documentation "How to implement" this feature. Any code sample, documentation?

I don't need any suggestion feature, so for now I just have the search bar (with keyboard) and showing the results as collection view.

enter image description here


Solution

  • I found the solution!!

    Since I didn't find many information specific for apple tv about this feature I'm letting here the solution could help someone :)

    We need to use the scope buttons

    example code:

    ...
    searchBar.scopeButtonTitles = ["Button 1", "Button 2"]
    searchBar.showsScopeBar = true
    searchBar.delegate = self
    ...
    
    extension SearchViewController: UISearchBarDelegate {
        ...
        func searchBar(_ searchBar: UISearchBar, selectedScopeButtonIndexDidChange selectedScope: Int) {
           print("clicked: \(selectedScope)")
        }
     }
    

    UISearchBar doc

    scopeButtonTitles doc

    Search bars UI doc