18

Hello I want the searchBar always visible, that is what I have:

searchController = UISearchController(searchResultsController: nil)
tableView.tableHeaderView = searchController.searchBar
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false

Is there way to accomplish this?

Thanks in advance

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105

5 Answers5

27

What about

override func viewDidAppear(_ animated: Bool) {
   super.viewDidAppear(animated)
   searchController.searchBar.becomeFirstResponder()
}

?

Have a look at Apple's example (source code): https://developer.apple.com/library/content/samplecode/TableSearch_UISearchController/Introduction/Intro.html

NB - from iOS 11 onwards you should use UINavigationController instead of the table header view:

        if #available(iOS 11.0, *) {
          // For iOS 11 and later, we place the search bar in the navigation bar.
          navigationController?.navigationBar.prefersLargeTitles = true
          navigationItem.searchController = searchController
          // We want the search bar visible all the time.
          navigationItem.hidesSearchBarWhenScrolling = false
      } else {
          // For iOS 10 and earlier, we place the search bar in the table view's header.
          tableView.tableHeaderView = searchController.searchBar
      }
gresch
  • 440
  • 5
  • 11
22

In viewDidLoad where you have your UISearchController setup:

navigationItem.hidesSearchBarWhenScrolling = false
Codetard
  • 2,441
  • 28
  • 34
Alexander
  • 1,424
  • 18
  • 23
  • 2
    It didn't work for me. I'm using ```SearchBar``` and embedding it in the TableView as ```tableHeaderView```. How to unhide it in such a scenario? – N a y y a r Mar 22 '20 at 20:23
3

You could try to put it in the header for the first section like this:

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { return self.searchController.searchBar }

I had to solve a very similar situation, and this is what I ended up with:

    searchController = UISearchController(searchResultsController: nil)
    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.delegate = self
    searchController.searchBar.sizeToFit()
    searchController.searchBar.tintColor = CARBONCOLOR
    searchController.hidesNavigationBarDuringPresentation = false
    self.definesPresentationContext = true

    let search = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "presentSearch")
    self.navigationItem.setRightBarButtonItem(search, animated: true)

With this code to present the searchController

func presentSearch()
{
    self.navigationController!.presentViewController(searchController, animated: true, completion: nil)
}

This would put a barbutton in the navbar which would present the searchController over the navbar. This is not exactly what I wanted, but turned out to be the simplest way to allow search from any point in the tableview. It also saves a bit of space, which is better. If you don't have a navbar, you can present from self.

Bjørn Ruthberg
  • 336
  • 1
  • 15
  • Sorry, I only tried this very quick without doing any searches. Once I tried searching I realized that reloading the table would also reload the searchbar. This makes the search impossibly difficult as you would have to remove and insert the search results manually to avoid reloading the section head. I'm not sure it can be done. At least I'm not willing to take the job of managing it. – Bjørn Ruthberg Jan 27 '16 at 08:28
1

The UISearchController class is supposed to be hidden when not in use (i.e. when no search is performed), so I would suggest to either use it in a way it was designed for or go another way.

In your case it might be more appropriate to implement a UISearchBar and e.g. configure it as an item within an (existing) UINavigationBar. That way, the search bar will be always visible.

Do you already have a view controller which holds a navigation bar in your view hierarchy? If so, try to set the search bar as an item and implement display of search results in another way as intended with the UISearchController pattern.

ff10
  • 3,046
  • 1
  • 32
  • 55
  • do you mind in take a look on the code here: https://github.com/nicolegs/MyDictionary –  Jan 11 '16 at 15:06
0

You're setting your searchBar in the tableHeaderView of your UITableView, the default behaviour is that the searchBar scroll with the UITableView, I'm afraid that you cannot change this in the way you did it.

But you can change the way you did it, you can use the UISearchDisplayController and manage the view yourself adn put it above your UITableViewin your hierarchy view, these two tutorials can help you a lot about how to achieve it and understand how it works:

I hope this help you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • Thank you Victor I will take a look on the links :) –  Jan 11 '16 at 21:42
  • Followed Ray Wenderlich, but search bar is invisible. It has worked in the past, though I didn't drag a search bar into the table view. It just appeared by magic. – magula Jul 22 '20 at 23:24