12

我的应用程序的表格视图没有占据全屏高度,因为我在底部允许 50px 用于横幅。

当我开始在搜索栏中输入时,搜索结果表视图更大;它填充了搜索栏和标签栏之间的所有可用屏幕空间。这意味着最后的搜索结果被横幅遮挡。

如何指定 UISearchDisplayController 使用的表格视图的大小?我看不到任何边界或框架属性。

编辑添加屏幕截图:

这就是在 IB 中设置表格视图的方式。它在距离合成标签栏 50px 处结束。

替代文字
(来源:lightwood.net

这就是内容正常显示的方式。我已经滚动到这里的最底部。 (来源:lightwood.net替代文字

这是搜索时的显示方式。同样,我已经滚动到最底部。如果我禁用横幅广告,我可以看到搜索显示表向下延伸到标签栏。

替代文字
(来源:lightwood.net

4

3 回答 3

51

解决这个问题的关键是找出何时更改表格视图的几何形状。来电:

[self.searchDisplayController.searchResultsTableView setFrame:someframe];

创建 UISearchDisplayController 后是徒劳的。答案是这个委托方法:

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

请注意,我也尝试过-searchDisplayController:didLoadSearchResultsTableView,但在那里没有用。您必须等到它显示出来才能调整它的大小。

另请注意,如果您只是简单地赋值tableView.frame = otherTableView.frame,搜索结果表会与其对应的搜索栏重叠,因此无法清除或取消搜索!

我的最终代码如下所示:

-(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;
}
于 2010-03-07T01:20:58.470 回答
1

我更新了代码以允许更深入的视图层次结构,但半透明封面视图的初始框架仍然占据了搜索栏下方的整个窗口。

-(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;
    }
}
于 2011-02-22T16:53:47.507 回答
0

我不确定我是否完全理解您所描述的内容,最好有截图。

听起来正在发生的事情是 UITableView 是屏幕的大小,而横幅与它的底部 50 像素重叠。所有 UIView 子级都有一个框架、边界和中心属性,它们继承自它们共同的 UIView 父级。

@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

您可以随心所欲地操纵这些属性,听起来您只需将边界调整为比屏幕小 50 像素,并进行一些数学运算来计算您的新中心。

于 2010-03-05T18:33:58.607 回答