Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController dismisses VC upon hitting cancel button

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?

like image 758
boidkan Avatar asked May 05 '15 18:05

boidkan


2 Answers

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 :-)

like image 165
Hernan Arber Avatar answered Nov 13 '22 12:11

Hernan Arber


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!

like image 43
Glenn Posadas Avatar answered Nov 13 '22 12:11

Glenn Posadas