Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController searchBar don't disappear when push viewcontroller

I'm using a UISearchController inside ma UIViewcontroller that contains a UITableView, I do this in viewDidLoad:

    self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.searchController.delegate = self;
    self.searchController.searchResultsUpdater = self;
    self.searchController.searchBar.delegate = self;
    self.searchController.dimsBackgroundDuringPresentation = NO;
    self.searchController.hidesNavigationBarDuringPresentation = NO;
    self.definesPresentationContext = NO;

when I push a button in the navbar i do this:

    self.tableView.contentOffset = CGPointMake(0, 0 - self.tableView.contentInset.top);
    self.tableView.tableHeaderView = self.searchController.searchBar;
    [self.searchController.searchBar becomeFirstResponder];

all works fine, but when I push a UIViewController from a row in the UITableView results the UISearchBar stay there and display also on the content of the other view, how can i dismiss when I push a view and present when I go back to see the result of the UITableView?

thanks

EDIT:

this is the code of the method didSelectRowAtIndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
    DetailListController *detailList = [[DetailListController alloc] init];
    [self.navigationController pushViewController:detailList animated:YES];
}
like image 752
Piero Avatar asked Oct 06 '15 16:10

Piero


1 Answers

You need to call this when you come back from DetailListController to your view controller (encapsulating in main thread for safety):

dispatch_async(dispatch_get_main_queue(), ^{
    self.searchController.active = NO;
});

You can also call this in viewWillDisappear: of your current view controller.

like image 109
Abhinav Avatar answered Sep 19 '22 14:09

Abhinav