14

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

3
  • removing controller.hidesNavigationBarDuringPresentation = false should also give you the behavior you are expecting Commented Apr 28, 2015 at 15:15
  • I've added 2 image which descripe the behaviour Commented Apr 28, 2015 at 15:18
  • set your searchController's hidesNavigationBarDuringPresentation to true and try toggle your ViewController's definesPresentationContext see the result. never hidesNavigationBarDuringPresentation to false and never put the search bar on navigationItem's titleView if you want the behavior you mentioning. Commented Apr 28, 2015 at 18:41

3 Answers 3

22

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;
Sign up to request clarification or add additional context in comments.

4 Comments

I've added 2 image which descripe the behaviour
Did you try the solution in this answer?
No but this will add to the top from the beginning and not have the same behaviour as in searchDisplayController
But i guess its okay if it is in tableHeaderVIew, but then it should be without the cancel button, but if i remove that it gives me the behaviour on the images
3

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

Comments

0

It has to do with the navigation bar.

func willPresentSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = true
}

func willDismissSearchController(searchController: UISearchController) {
    self.navigationController?.navigationBar.translucent = false
}

If you do this, then you will be able to add it to the table view header AND get the baked-in search controller animation!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.