Search code examples
iosswiftinterface-builderuisearchcontroller

Hide UISearchController when segue to another view


I'd like to hide my search bar from the tableHeaderView when I segue. How can I do this? I create the search bar as follows:

override func viewDidLoad() {
   ...
   resultSearchController = ({
        let searchController = UISearchController(searchResultsController: nil)       //  The results of the search will be presented in the current Table View, so the searchResultsController parameter of the UISearchController init method is set to nil.

        searchController.delegate = self        
        searchController.searchBar.delegate = self          // Without this selectedScopeButtonIndexDidChange won't get called.
        searchController.searchResultsUpdater = self
        searchController.hidesNavigationBarDuringPresentation = true    // NOTE: if false and tapping on a reminder to go the its details and then back then the titles may be screwed up.
        searchController.dimsBackgroundDuringPresentation = false       // NOTE: If true it would result in the filtered list not being scrollable.
        searchController.hidesBottomBarWhenPushed = true
        searchController.searchBar.sizeToFit()
        self.definesPresentationContext = false    

        searchController.searchBar.scopeButtonTitles = SCOPEBAR_OPTIONS.descriptionArray
        tableView.tableHeaderView = searchController.searchBar
        return searchController
    })()

I tried to simply set it to nil like this, but no luck:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    tableView.tableHeaderView = nil
    ...
}

Solution

  • To dismiss searchcontroller use below method

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
      searchController.active = NO;
    }