6

We have a search bar in the table header. When the user taps on it twice quickly on iOS 7, it disappears. Does anyone have any suggestions what we are doing wrong?

Jason Hocker
  • 6,879
  • 9
  • 46
  • 79
  • Is it going under Nav bar? I have search bar on few views and works fine. – User382 Oct 07 '13 at 21:39
  • I am also getting the same issue. I have tried by removing following lines from code. self.edgesForExtendedLayout=UIRectEdgeNone; self.navigationController.navigationBar.translucent = NO; It solves the issue. But the UI gets disturb. – Priya Oct 08 '13 at 10:50

2 Answers2

9

After lots of trial and errors, I found that when searchDisplayController ends search, searchbar gets disappear, so I have reinserted the searchbar to table header and it worked for me.

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    self.searchingFetchedResultsController = nil;
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView];
    }
    return;
}

Hope this helps.

Priya
  • 637
  • 4
  • 5
  • I got the same bug too. – Chen Pang Oct 10 '13 at 23:07
  • thanks Priya, that seems to work so basically: - (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller { if (IS_IOS7_OR_GREATER) { [self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView]; } } – Richie Hyatt Oct 22 '13 at 15:48
  • Surprisingly it really helped, however dirty the solution is and I really don't like that I have to use it as well. The issue arises when user taps again into the already-animating search view, where the other tap is received by empty results table of the searchDisplayController – causing it to close immediately. – igraczech Mar 25 '14 at 13:51
7

(I posted this same answer to Troubles with UISearchBar \ UISearchDisplayViewController, which seems like a duplicate of this question.)

I encountered the same issue, and noticed that searchDisplayControllerDidEndSearch was being called twice. The first time, the superview of self.searchDisplayController.searchBar is the UITableView, and the second time it's still a UIView.

With Priya's answer, I worry about unintended consequences or unneeded overhead from re-inserting the subview every time the search bar is double-tapped, and I also worry about it breaking with future iOS versions. Fortunately, we can take advantage of the superview strangeness like this:

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
    if (self.tableView != self.searchDisplayController.searchBar.superview) {
        [self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView];
    }
}

If I had to guess what was happening, the UISearchBar is automatically creating a temporary UIView as its superview when it's active – this is the view seen when the search is being performed. While the UISearchBar is being dismissed, the superview gets set back to be the UITableView it had before, unless it gets dismissed so quickly that it was never properly initialized, in which case it cleans up improperly and the UITableView never gets the UISearchBar back as its child.

This solution still isn't ideal, and I think Apple must be doing something different in its own apps because their search bar UX feels a bit better. I think it would be better not to handle the second tap in the first place until the UISearchBar was ready. I tried using the other UISearchBarDelegate methods to do this, but I couldn't find an appropriate hook to override the current behavior.

Community
  • 1
  • 1
lehrblogger
  • 191
  • 2
  • 5
  • 1
    Just ran into this bug myself. What if you check if `window` is nil? That way, you know it's not in the view hierarchy and you don't have to hard code the equality check. – Eric Amorde Mar 12 '15 at 22:56