Question

I'm encountering this weird bug regarding UISearchDisplayController's table view, which occurs only in iOS 6. I just created my tableview in a nib file and then programmatically added a search bar above it and a search display controller to filter data in the table view:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.mpSearchBar = [[MPSearchBar alloc] initWithFrame:CGRectMake(0, 0, 250, 44)];
    self.mpSearchBar.placeholder =@"Card Search";

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.mpSearchBar contentsController:self];
    self.searchController.delegate = self;
    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;  

    self.resultTableView.delegate = self;
    self.resultTableView.dataSource = self;
    [self.resultTableView reloadData];

}

When I first enter some query in the search bar, the search display controller filters the data and the controller's result table view works fine. However when I tap the clear button in the search bar and type something else, the result table view which contains the new set of filtered data can no long scrollable. What's weird is, when I try to log the frame and contentSize of the table view, the content size height is larger than the frame height, as it's supposed to be:

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

    tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
    tableView.contentInset = UIEdgeInsetsZero;
    [tableView hideEmptySeparators];

    if (IOS_EQUAL_OR_NEWER_THAN_7){
        tableView.separatorInset = UIEdgeInsetsZero;
    }

    NSLog(@"Frame height %f, Content height %f", tableView.frameHeight, tableView.contentSize.height);
}

And this is what I get from the log:

Frame height 504.000000, Content height 1402.000000

This happens only when I test on iOS 6 devices and I have no clue how to debug this issue.

Please suggest and thanks.

Was it helpful?

Solution

Turns out this is a (not well) known issue of UISearchController's table view in iOS 6. My temporary solution is to get the contentSize from willShowSearchResultsTableView and programmatically set it to the table view in viewDidLayoutSubviews:

- (void) viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    if ([self.searchController isActive]){
        // fix wrong content size due to search bar glitch in iOS 6
        self.searchController.searchResultsTableView.contentSize = newContentSize;
    }
}

Hope this helps anyone who encounters the same problem as mine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top