Search code examples
iosuisearchcontroller

Does UISearchContainerViewController work on iOS


UISearchContainerViewController exists both on tvOS and on iOS.

Apple has sample code showing how to use it in tvOS: they configure a UISearchController, hand it to a UISearchContainerViewController, wrap that in a UINavigationController, and make it one of a UITabBarController's children.

But I have never seen an example of UISearchContainerViewController on iOS, and I can't make it work there. For example, I do exactly what Apple does, except that I push the UISearchContainerViewController onto a navigation stack, or I wrap it in a navigation controller and present it, or whatever; and there's no search field so the whole thing is useless.

Has anyone ever gotten UISearchContainerViewController to do anything useful on iOS?


Solution

  • I was looking for answer to the same question. But I got this working like that :

    Base VC

        let vc = SearchContainerVC()
        let nav = UINavigationController(rootViewController: vc)
        self.present(nav, animated: true, completion: nil)
    

    SearchContainerVC

    class SearchContainerVC: UISearchContainerViewController {
    
        init() {
            let searchResultsTableVC = SearchResultsTableViewController()
            let searchVC = UISearchController(searchResultsController: searchResultsTableVC)
            searchVC.searchBar.searchBarStyle = .minimal
            searchVC.searchBar.showsCancelButton = true
            searchVC.searchBar.delegate = searchResultsTableVC
            searchVC.searchResultsUpdater = searchResultsTableVC
    
            let searchBar = searchVC.searchBar
            searchBar.delegate = searchResultsTableVC
            searchBar.sizeToFit()
            searchBar.placeholder = "Search for something"
    
            searchVC.hidesNavigationBarDuringPresentation = false
            searchVC.dimsBackgroundDuringPresentation = true
    
            super.init(searchController: searchVC)
    
            navigationItem.titleView = searchBar
        }
    }
    

    SearchResultsTableViewController

    class SearchResultsTableViewController: UITableViewController {
        // I use a Table to show search items
    }