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

。_饼干妹妹 提交于 2019-12-29 04:18:26

问题


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)


回答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;
}



回答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;
    }
}



回答3:


I'm not sure I completely understand what you're describing, it would be nice to have a screenshot.

It sounds like what's happening is the UITableView is the size of the screen and the banner is overlapping the bottom 50 pixels of it. All UIView children have a frame, bounds, and center properties they inherit from their common UIView parent.

@interface UIView(UIViewGeometry)

// animatable. do not use frame if view is transformed since it will not correctly reflect the actual location of the view. use bounds + center instead.
@property(nonatomic) CGRect            frame;

// use bounds/center and not frame if non-identity transform. if bounds dimension is odd, center may be have fractional part
@property(nonatomic) CGRect            bounds;      // default bounds is zero origin, frame size. animatable
@property(nonatomic) CGPoint           center;      // center is center of frame. animatable
@property(nonatomic) CGAffineTransform transform;   // default is CGAffineTransformIdentity. animatable
// ... from UIView.h

You can manipulate these properties as you like, it sounds like you simply need to adjust the bounds to be 50 pixels smaller than the screen, and do some math to calculate your new center.



来源:https://stackoverflow.com/questions/2388906/iphone-sdk-setting-the-size-of-uisearchdisplaycontrollers-table-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!