UISearchController dismisses VC upon hitting cancel button
Asked Answered
C

2

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?

Callery answered 5/5, 2015 at 18:7 Comment(8)
What happens when you don't include the line setting active to false and you hit the cancel button?Wampumpeag
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.Callery
It dismisses when there are no search results, or dismisses when there are no search results and you hit cancel?Wampumpeag
No search results and you hit cancel. Any ideas?Callery
Found this very thorough resource. Maybe the line with definesPresentationContext = true is the clincher.Wampumpeag
Thanks, I will go through it!Callery
You're welcome; Let me know if adding that line helps and I'll add it as an answer below.Wampumpeag
@boidkan, I am also facing the same issue, did you got any solution for this?Hallowed
M
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 :-)

Mcminn answered 9/3, 2017 at 9:12 Comment(1)
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.Harp
L
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://mcmap.net/q/234483/-uisearchbar-increases-navigation-bar-height-in-ios-11

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!

Lenna answered 22/11, 2018 at 18:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.