0

I'm trying to set up a UISearchController with a custom UITableViewController for the results. In my main UITableViewController I have a segue from my main UITableViewController to a detail controller, if I pass in a model object to the detail controller with values the search bar is initially hidden but if I scroll it displays (similar to when loading the Inbox in the default iOS mail app). If no values are passed to the results controller then the search bar is displayed.

I am creating the search results controller in the detail view controller as follows:

  func configureSearchController() {

      let storyboard = UIStoryboard(name: "Main", bundle: nil)
      guard let resultsController = storyboard.instantiateViewController(withIdentifier: "resultsController") as? SearchResultsTableViewController else {
          return
      }

      self.searchController = UISearchController(searchResultsController: resultsController)

      //  results controller handles search results
      searchController.searchResultsUpdater = resultsController
      searchController.searchBar.delegate = resultsController

      searchController.searchBar.placeholder = "Search items"
      searchController.searchBar.returnKeyType = .done
      searchController.searchBar.sizeToFit()
      searchController.edgesForExtendedLayout = []

      self.definesPresentationContext = true
      self.navigationItem.searchController = searchController
  }

This is what happens:

enter image description here

How can I make it so the search bar always shows on the detail controller?

Rizwan Saeed
  • 83
  • 1
  • 1
  • 7
  • Sorry I meant detail controller, have updated. Thanks – Rizwan Saeed Dec 24 '19 at 01:15
  • Ok so then you would just need to scroll in code so the search bar shows? But I still don’t understand really because if the first screen is your results I expect to see the search bar there too. – matt Dec 24 '19 at 01:50
  • 1
    Did you try `navigationItem.hidesSearchBarWhenScrolling = false`? – Don Dec 24 '19 at 03:45

0 Answers0