Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchDisplayController's searchResultsTableView's ContentSize is incorrect. Bug in iOS 7?

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?

like image 577
Alex Muller Avatar asked Sep 28 '13 17:09

Alex Muller


2 Answers

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];

}
like image 181
Rebecca Duhard Avatar answered Nov 04 '22 03:11

Rebecca Duhard


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];
}
like image 43
Allen Avatar answered Nov 04 '22 05:11

Allen