5

Is it possible to use the UISearchController without the SearchBar being displayed in the Navigation Controller? I basically want the SearchBar to stay on the screen and display the TableView beneath it:

Mockup

What I was trying to do is this: I've created a UITableViewController on the storyboard and linked it to the custom NPTableViewController class. Then I did this:

    let resultsTable = storyboard!.instantiateViewController(withIdentifier: "LocationSearch") as! NPTableViewController
    searchController = UISearchController(searchResultsController: resultsTable)
    searchController?.searchResultsUpdater = resultsTable

    let searchBar = searchController!.searchBar
    searchBar.sizeToFit()
    searchBar.placeholder = "Search for places"
    searchBar.frame.origin.y = 100.0
    self.view.addSubview(searchBar)

Now when I run it, the searchBar is displayed but when I click on it the background dims, the searchBar disappears my resultsTable is also not displayed.

Codey
  • 1,131
  • 2
  • 15
  • 34

1 Answers1

4

The solution I came up with is actually very simple. Instead of using UISearchController, I used a normal UISeachBar and UITableView. Since UISearchController doesn't do so much to help you, the work is pretty much the same if I kind of build my own SearchController.

Codey
  • 1,131
  • 2
  • 15
  • 34
  • But adding a UISearchBar automatically adds a deprecated UISearchDisplayController which will crash your apps in production (didn't used to but does now). You may have to manually delete the UISearchDisplayController from any XIB or storyboard files. – Victor Engel Jun 25 '23 at 15:34