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