iPhone SDK: Setting the size of UISearchDisplayController's table view
Solution 1
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;
}
Solution 2
I updated the code to allow for deeper view hierarchies, but the initial frame of the semi-transparent cover view still takes up the entire window below the search bar.
-(void)searchDisplayController: (UISearchDisplayController*)controller
didShowSearchResultsTableView: (UITableView*)tableView
{
if ( [controller.searchBar.superview isKindOfClass: [UITableView class]] )
{
UITableView* staticTableView = (UITableView*)controller.searchBar.superview;
CGRect f = [tableView.superview convertRect: staticTableView.frame fromView: staticTableView.superview];
CGRect s = controller.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;
}
}
winterandrea
Updated on June 14, 2022Comments
-
winterandrea about 2 years
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 results table view is larger; it fills all available screen space between the search bar and the tab bar. This means that the very last search result is obscured by the banner.
How do I specify the size of the table view used by UISearchDisplayController? There's no bounds or frame property that I can see.
EDIT TO ADD SCREENSHOTS:
This is how the table view is set up in IB. It ends 50px short of the synthesized tab bar.
(source: lightwood.net)This is how content displays normally. I've scrolled to the very bottom here.
(source: lightwood.net)This is how it displays when searching. Again, I've scrolled to the very bottom. If I disable the banner ad, I can see that the search display table spreads right down to the tab bar.
(source: lightwood.net) -
winterandrea over 14 yearsI've added some screenshots. At no point am I setting the geometry for the search table display. If I can figure out where to do that, it's probably the answer... but the searchResultsTableView property of a UISearchDisplayController is read-only.
-
slf over 14 yearsThe searchResultsTableView property is readonly, but that just means you can't change the handle on the controller, you should be able to manipulate the tableview itself.
-
slf over 14 yearsthere are two UITableViews, the one you are creating, and the one created by the search results controller. you'll need to manipulate both if you want them both to have the same effect
-
George Asda about 11 yearsThanks for that mate. it was driving me crazy!
-
user2872856 over 7 yearsHow to declare the
someframe
? -
BigHeadCreations over 6 years@user2872856
tableView.frame
is a CGRect, so you can do:CGRect someframe = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);