8

So, I am currently trying to replace the depricated searchDisplayController in one of my projects with UISearchController and I am running into this problem.

If there are no results in the search (the UITableView is empty) the whole ViewController is dismissed. This does not happen when the search results are not empty. I wan't to make it clear I am not using a UITableViewController. Instead I have a regular VC with a UITableView in it.

Here is some of my code:

var resultSearchController = UISearchController()
override func viewDidLoad() {
    super.viewDidLoad()
    self.resultSearchController = ({
        let controller = UISearchController(searchResultsController: nil)
        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.delegate = self
        controller.searchBar.delegate = self
        self.studentTable.tableHeaderView = controller.searchBar
        return controller
    })()
    ....
}

Now, if I add this function to the equation the cancel button always dismisses the VC.

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
    resultSearchController.active = false
}

So why exactly does setting the searchController.active = false dismiss the VC? Is it because it is using the same UITableView as the VC? I believe that the old searchDisplayController would just display a UITableView over the one being used. If this is the case is there a way to override the dismissVC?

boidkan
  • 4,691
  • 5
  • 29
  • 43
  • What happens when you don't include the line setting `active` to `false` and you hit the cancel button? – shim May 05 '15 at 18:31
  • When it is not included it only dismisses the VC when there are no search results as the question states. If it is included then it always does it when the cancel button is hit. – boidkan May 05 '15 at 19:32
  • It dismisses when there are no search results, or dismisses when there are no search results and you hit cancel? – shim May 05 '15 at 19:48
  • No search results and you hit cancel. Any ideas? – boidkan May 05 '15 at 19:53
  • Found [this very thorough resource](https://github.com/codepath/ios_guides/wiki/Search-Bar-Guide). Maybe the line with `definesPresentationContext = true` is the clincher. – shim May 06 '15 at 03:25
  • Thanks, I will go through it! – boidkan May 06 '15 at 15:43
  • You're welcome; Let me know if adding that line helps and I'll add it as an answer below. – shim May 06 '15 at 17:14
  • @boidkan, I am also facing the same issue, did you got any solution for this? – user2526811 Dec 16 '16 at 09:11

2 Answers2

4

this is also Happening to me. The Way I Solve it is by Replacing:

   resultSearchController.active = false

with

    resultSearchController.searchBar.text = ""
    resultSearchController.searchBar.resignFirstResponder()

I Hope this helps you :-)

Hernan Arber
  • 1,626
  • 3
  • 22
  • 35
  • This solution keeps searchController still active which means, if you have attached SearchBar to the tableview with pull-down-to-refresh enabled, refresh indicator wont be visible; As alternate workaround, remove existing searchBar from superview and create & attach new one to the tableview. – Delorean Jun 06 '17 at 09:38
1

2018 Just wanna share the fruits of my 1-2 hours debugging.

I had multiple issues with using UISearchController with UITabBarController, namely:

  1. This one, this very question of the OP. Hitting cancel button dismisses the screen that is presenting the searchController.

  2. The tab (or the screen) becomes black, Tab Bar and UISearchController giving black screen

  3. Using UISearchController inside the title view of the navigation bar of UINavigationController in both iOS 10, 11, and 12, like this questions. UISearchBar increases navigation bar height in iOS 11

And for the solution for #3, since we're already here: https://stackoverflow.com/a/53264329/3231194

Finally, the ONLY solution that I have been seeing all this time is adding this code:

self.definesPresentationContext = true

The issue is that I was putting this in a wrong function.

Remember, that solution solved the #1, and #2 problem that I had. Nothing more, nothing less.

Where to add that? Inside the viewDidAppear. That's it!

Glenn Posadas
  • 12,555
  • 6
  • 54
  • 95