9

I'm having a hard time with my SearchDisplayViewController on iOS 7. I have a searchBar hidden over a UITableViewController, like

self.tableView.tableHeaderView = searchBar;

Problem is that when I tap on the searchBar to type in something, then the view starts greying out, and I quickly tap the screen in a random point to dismiss it, coming back to the tableView, the searchBar disappears. Totally. Only on iOS 7 though.

Debugging it, the frame is always the same: 0,0,320,44. But the bar is invisible!

Also tried to do

self.tableView.contentOffset = CGPointMake(0,self.searchDisplayController.searchBar.frame.size.height);

still disappears when I do it quickly.

On iOS 6 it works just fine. Problem is only with iOS 7 as far as I'm seeing.

I don't know what it depends on, has anyone encountered the same problem I have?

Phillip
  • 4,276
  • 7
  • 42
  • 74
  • If you refer to the tableView, yes it is – Phillip Sep 23 '13 at 18:09
  • oop. uitableviewcontroller is right there in the question, sorry. in general, the search bar doesn't go in the tableview header. that's the problem. tableview in a tablevc will grey when search becomes active (in any os version, i thought). the header is part of the table view. – danh Sep 23 '13 at 18:12
  • Hm i got it. What i wanted to do was hide the searchBar over the navigationController, so when the user scrolls down from there the searchBar appears, just like in Mail app. It happens only with iOS 7 though, and only if I quickly tap on it after it becomes active. On iOS 6 it works fine even if do it quckly – Phillip Sep 23 '13 at 18:14
  • I am experiencing the same issue. Are you getting any crashes related to the search bar? i.e. from tapping it when it is "invisible" – Patrick Goley Sep 23 '13 at 18:23
  • No, not any problems at all. It's just.. invisible – Phillip Sep 23 '13 at 18:26
  • We are seeing the same issue. tap the text area of the UISearchBar twice quickly and the search bar disappears completely. This has worked fine on all previous iOS releases, just not on iOS7! We have another screen that is built with a XIB file that does not exhibit this problem on iOS7, the search bar does not disappear. I will try to figure out what is different besides that on the one that doesn't work the UISearchBar is added in code and the one that does work is configured in a XIB file. I will post an answer if I find something. -rrh – Richie Hyatt Oct 22 '13 at 15:16
  • a workaround is found here, need to add the search back on iOS7 when the search ends. http://stackoverflow.com/questions/19232653/double-tap-uisearchbar-with-search-delegate-on-ios-7-causes-uisearchbar-to-disap – Richie Hyatt Oct 22 '13 at 15:50
  • Thanks for your comment Richie, I found that link very useful: in fact I'm not getting the bug anymore, at least on iOS 7. So, if you want to post the answer, I will be very glad to accept it! – Phillip Oct 22 '13 at 20:52

4 Answers4

18

As of Double tap UISearchBar with search delegate on iOS 7 causes UISearchBar to disappear, I found the workaround to actually work and solved the bug - for now.

- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
    if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
        [self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView];
    }
}
Community
  • 1
  • 1
Phillip
  • 4,276
  • 7
  • 42
  • 74
4

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 the accepted 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.

lehrblogger
  • 191
  • 2
  • 5
3

I had the same problem with iOS 7 and I solved it from the apple documentation. The error most people do is that they associate the UISearchBar variable to the self.searchDisplayController.searchBar as the same...! NO NO..! They are 2 different things!!! UISearchBar should be declared and initialized and then wrapped into self.searchDisplayController as searchBar then later wrapped into self.tableView.tableHeaderView by so doing it will not disappear!!!

self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

self.searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

self.searchDisplayController.delegate = self;

self.searchDisplayController.searchResultsDataSource = self;

self.searchDisplayController.searchResultsDelegate = self;

[self.searchBar setPlaceholder:@"search the hell in me"];

self.tableView.tableHeaderView = self.searchDisplayController.searchBar; 
bikram990
  • 1,085
  • 1
  • 14
  • 36
Bob Godwin
  • 78
  • 2
  • Now I'm only wondering how to do this right in IB (without migrating to storyboards right now)? – oh7lzb Jul 15 '14 at 17:17
0

More refined approach for @lehrblogger solution:

- (void)addSearchDisplayControllerBackToTableView {
    if ([self.searchDisplayController.searchBar isDescendantOfView:self.tableView] == NO) {
        NSLog(@"Search bar is not in current table view, will add it back");
        [self.tableView insertSubview:self.searchDisplayController.searchBar aboveSubview:self.tableView];
        [self.searchDisplayController setActive:NO animated:YES];
    }
}

Reason for this approach: While searching the search bar is moved to search container and the superview of search bar is always some other view other than current table view.

Note: This will dismiss the search, because user tapped more than once on search bar.

bikram990
  • 1,085
  • 1
  • 14
  • 36