Invalid offset for the contents of the UISearchDisplayController table view after hiding the keyboard

I have a UISearchDisplayController and it displays the results in a table. When I try to scroll through the table, the contents will be exactly _keyboardHeight higher than it should be. This leads to a false lower bias. There are> 50 elements in the table view, so there should be no space, as shown below.

enter image description here

+8
ios objective-c uitableview uisearchdisplaycontroller
source share
2 answers

I solved this by adding an NSNotificationCenter listener

 - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView { //this is to handle strange tableview scroll offsets when scrolling the search results [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil]; } 

Remember to remove the listener.

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

Adjust contents of table contents in notification method

 - (void)keyboardDidHide:(NSNotification *)notification { if (!self.searchDisplayController.active) { return; } NSDictionary *info = [notification userInfo]; NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; CGSize KeyboardSize = [avalue CGRectValue].size; CGFloat _keyboardHeight; UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (UIDeviceOrientationIsLandscape(orientation)) { _keyboardHeight = KeyboardSize.width; } else { _keyboardHeight = KeyboardSize.height; } UITableView *tv = self.searchDisplayController.searchResultsTableView; CGSize s = tv.contentSize; s.height -= _keyboardHeight; tv.contentSize = s; } 
+12
source share

Here is an easier and more convenient way to do this based on the Hlung link:

 - (void)searchDisplayController:(UISearchDisplayController *)controller willShowSearchResultsTableView:(UITableView *)tableView { [tableView setContentInset:UIEdgeInsetsZero]; [tableView setScrollIndicatorInsets:UIEdgeInsetsZero]; } 

Note. The original answer uses the NSNotificationCenter to get the same results.

+12
source share

All Articles