27

The below problem only occurs on an iOS 6.0/6.1 application running on an iOS 7.0+ device.

So I have a UISearchDisplayController that searches our API and returns data. This all works, and everything is displayed as we want. The only problem we are seeing is that after the content has populated the searchResultsTableView, it seems as though when the keyboard is initially hidden, the contentSize of the searchResultsTableView is much larger than the data, and actually seems to be the size of the keyboard. When I enter the search bar, and show the keyboard and hit 'Search' again (just to hide the keyboard), the contentSize then adjusts correctly to only fill the screen, and nothing more. Below is a screen shot of what I am talking about with the initial tableView population.

The white is the table data, and the gray/cream color is the extra tableView space.

Any ideas on how to fix this?

Community
  • 1
  • 1
Alex Muller
  • 1,565
  • 4
  • 23
  • 42

2 Answers2

62

I had this exact problem. The solution posted on the developer forums here worked for me. Not sure if it's a bug in iOS 7 or just that they changed the way they're doing things but this is the only solution that I found solved my problem.

Solution from the forum post for the lazy:

- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];

}



- (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];

}



- (void) keyboardWillHide {

    UITableView *tableView = [[self searchDisplayController] searchResultsTableView];

    [tableView setContentInset:UIEdgeInsetsZero];

    [tableView setScrollIndicatorInsets:UIEdgeInsetsZero];

}
Rebecca Duhard
  • 916
  • 9
  • 9
  • Worked like a charm! However, I am very confused that if I add observer in `viewDidLoad` and remove it in `dealloc`. The content and scroll indicator insets became negative. Not sure why this happened. – David Liu Aug 05 '14 at 14:18
  • for iOS 8 you should check @Allen answer below – Flores Robles Jun 15 '15 at 22:43
8

This system bug remains in iOS 8, and the accept answer's solution doesn't work anymore. So, you should use the following solution:

-(void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

-(void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)keyboardWillHide:(NSNotification*)notification {
    CGFloat height = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    UITableView *tableView = [[self searchDisplayController] searchResultsTableView];
    UIEdgeInsets inset;
    [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 ? (inset = UIEdgeInsetsMake(0, 0, height, 0)) : (inset = UIEdgeInsetsZero);
    [tableView setContentInset:inset];
    [tableView setScrollIndicatorInsets:inset];
}
Allen
  • 6,745
  • 5
  • 41
  • 59
  • 1
    This works perfectly! The accepted answer on iOS 8 will prevent the bottom part of the tableview to be scrolled into view. – wilsontgh Mar 10 '15 at 16:08