30

I want to close my UISearchDisplayController when the user clicks the "Search" button since I'm loading new data from the web. How do I close the controller programatically? I already have the proper method called, but don't know how to do it.

I thought that the below would work, but I'm wrong.

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self.searchDisplayController finalize];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Raphael Caixeta
  • 7,808
  • 9
  • 51
  • 76

3 Answers3

69
[self.searchDisplayController setActive:NO animated:YES];

Enjoy.

Raphael Caixeta
  • 7,808
  • 9
  • 51
  • 76
  • 1
    Thank you very much.. working great and helpful in 2012 too :) – Xtrician Jun 30 '12 at 08:08
  • this didn't work for me. I said once user tap somewhere in -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {[self.searchDisplayController setActive:NO animated:YES];} But this did not let it go away. This method got called but no result. – coolcool1994 Jul 03 '13 at 20:27
  • Perfect. I'm using this in viewWillDisappear. Just what I needed. – Benjamin Stark Dec 19 '13 at 23:01
3

You need to make sure you set active to false on main thread:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.searchDisplayController setActive:NO animated:YES];
});
Appulus
  • 18,630
  • 11
  • 38
  • 46
LiorR
  • 31
  • 2
2
func setActive(_ visible: Bool,
  animated animated;: Bool)

If you are using swift.

xipengyang
  • 96
  • 1
  • 5