Search code examples
iosobjective-cuisearchcontroller

iOS UISearchController crash: Application tried to present modal view controller on itself


According to crashlytics the following crash is occurring (rarely).

Application tried to present modal view controller on itself. Presenting controller is .

I can't replicate this issue at all. This is how I setup my UISearch Controller.

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

    self.tableView.tableHeaderView = self.searchController.searchBar;
    self.definesPresentationContext = YES;

Any help is appreciated because I am all out of ideas. I will post more code if needed.


Solution

  • I had that issue when I updated to iOS 11. My scenario was, that I had a Textfield, and when the user started to edit that, a search-view, essentially a tableview with a searchbar as header popped up and once a tableview cell was tapped it should close.

    The problem seems to be that since iOS 11, the OS tries to restore the firstResponder state. Long story short.

    It helped when I added active = NO, to my did select method, like so

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       self.searchController.active = NO; // Add this !
       ...
    
       [self dismissViewControllerAnimated:YES completion:nil];
    }