Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController in navigationBar

I'm trying to use the new UISearchController in my tableViewController.

However I'm a bit confused on how it can move up in the navigationController when I press the searchBar like it did with the old searchDisplayController?

At the moment it just stay in the tableHeader.

Here is my code:

    self.teamSearchController = ({
        let controller = UISearchController(searchResultsController: nil)

        controller.searchBar.searchBarStyle = UISearchBarStyle.Minimal
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
       controller.searchBar.showsScopeBar = true
        self.tableView.tableHeaderView = controller.searchBar

        return controller
    })()

Controller:

enter image description here

When I click on searchbar:

enter image description here

like image 320
Peter Pik Avatar asked Apr 28 '15 14:04

Peter Pik


2 Answers

You can place the UISearchBar of UISearchController in the navigation bar instead of table header

self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar;

self.definesPresentationContext = YES;
like image 57
Praveen Gowda I V Avatar answered Sep 28 '22 17:09

Praveen Gowda I V


Swift version:

self.searchController.hidesNavigationBarDuringPresentation = false
self.searchController.searchBar.searchBarStyle = UISearchBarStyle.Minimal

// Include the search bar within the navigation bar.
self.navigationItem.titleView = self.searchController.searchBar
self.definesPresentationContext = true
like image 27
fatihyildizhan Avatar answered Sep 28 '22 17:09

fatihyildizhan