iPhone SDK: Setting the size of UISearchDisplayController's table view

前端 未结 3 854
天命终不由人
天命终不由人 2020-12-14 21:20

My app\'s table view does not occupy the full screen height, as I\'ve allowed 50px at the bottom for a banner.

When I begin typing in the search bar, the search resu

3条回答
  •  星月不相逢
    2020-12-14 22:24

    The key to solving this one was finding out when to change the geometry of the table view. Calling:

    [self.searchDisplayController.searchResultsTableView setFrame:someframe];
    

    after creating the UISearchDisplayController was futile. The answer was this delegate method:

    -(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
        tableView.frame = someframe;
    }
    

    Note, I had also tried -searchDisplayController:didLoadSearchResultsTableView but it did no good in there. You have to wait until it's displayed to resize it.

    Also note that if you simply assign tableView.frame = otherTableView.frame, the search results table overlaps its corresponding search bar, so it is impossible to clear or cancel the search!

    My final code looked like this:

    -(void)searchDisplayController:(UISearchDisplayController *)controller didShowSearchResultsTableView:(UITableView *)tableView {
    
        CGRect f = self.masterTableView.frame;  // The tableView the search replaces
        CGRect s = self.searchDisplayController.searchBar.frame;
        CGRect newFrame = CGRectMake(f.origin.x,
                                     f.origin.y + s.size.height,
                                     f.size.width,
                                     f.size.height - s.size.height);
    
        tableView.frame = newFrame;
    }
    

提交回复
热议问题