How do you dismiss a UISearchController ? (iOS 8 and follow)
Asked Answered
G

5

76

This must be trivial, but I can't find how you're supposed to dismiss a UISearchController programmatically?

Note that it's the new UISearchController (introduced in 2014 with iOS 8), not the UISearchDisplayController.

So far here's what I've got

// Dismiss the search tableview
searchController.dismissViewControllerAnimated()
// Clear the Search bar text
searchController.active = false

But I still have the cancel button and can't get rid of it.

Gallfly answered 10/2, 2015 at 17:48 Comment(0)
G
186

OK so after more testing, turns out you just have to set:

searchController.active = false
// or swift 4+
searchController.isActive = false

This is the first thing I tried but I called it in one of the UISearchControllerDelegate methods which didn't work (probably should have called it with dispatch_async (halbano's answer seems to confirm that)).

Anyway, since I couldn't find that answer online, I'm answering my own question, I hope that it'll help someone.

Gallfly answered 10/2, 2015 at 17:48 Comment(3)
This actually works! Idk how just changing a variable actually triggers UI redrawing but it worksMonophyletic
@Monophyletic Most likely because there is some KVO (Key Value Observing) going onGallfly
Plus it calls UISearchControllerDelegate's didDismissSearchController(searchController) method.Northnortheast
G
25

Did you have this problem when you try to dismiss search controller after segueing to another view? I have encountered this problem too. I think you might want to use

self.definesPresentationContext = true 

in the view controller that presents the UISearchController as per this post UISearchController not dismissed when View is pushed. It works for me.

Globefish answered 2/12, 2015 at 6:32 Comment(0)
D
11

I was presenting the mine embed on a navigation bar. The code that works for me was:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.searchController setActive:NO];
        self.navigationController.navigationBar.topItem.title = @"MYTITLE".uppercaseString;
        self.navigationItem.titleView = nil;
    });
}

Hope it helps someone.

Distributor answered 12/12, 2015 at 23:19 Comment(3)
wasnt the exact code, but the dispatch async pointed me in the right direction!Glib
Good! Also get the main thread to handle UI elements is mandatory, maybe this was the magic.Distributor
@Distributor this delegate method is called on the main thread. I believe there's crosstalk between the search bar animation and whatever the listener is doing here that is the cause of conflict. this will perform the responding code on the next run loop.Towns
B
7

SWIFT 4+

searchController.isActive = false
Bowker answered 2/3, 2018 at 13:10 Comment(0)
G
2

I had this problem using the search and interactionController, solved after just include the line: self.dismissViewControllerAnimated(false, completion: nil)

Open the interaction and clear the search without changes in the delegate.

Gala answered 27/10, 2016 at 13:18 Comment(1)
The problem is this won't trigger some delegate functions like willDismissSearchController which could be necessary.Modie

© 2022 - 2024 — McMap. All rights reserved.