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

标签 iphone uitableview uisearchdisplaycontroller

我的应用程序的表格 View 不占据整个屏幕高度,因为我允许底部有 50 像素的横幅。

当我开始在搜索栏中输入内容时,搜索结果表格 View 会变大;它填充了搜索栏和选项卡栏之间的所有可用屏幕空间。这意味着最后的搜索结果被横幅遮盖了。

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

编辑以添加屏幕截图:

这就是 IB 中 TableView 的设置方式。它结束于距离合成标签栏 50px 的地方。

alt text
(来源:lightwood.net)

这是内容正常显示的方式。我已经滚动到这里的最底部。 alt text
(来源:lightwood.net)

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

alt text
(来源:lightwood.net)

最佳答案

解决这个问题的关键是找出何时更改表格 View 的几何形状。调用:

[self.searchDisplayController.searchResultsTableView setFrame:someframe];

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

-(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;
}

关于iPhone SDK : Setting the size of UISearchDisplayController's table view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2388906/

相关文章:

iphone - Objective-C 中的数组变量访问问题

android - 使用android:versionCode和Bundle版本> 1发布Android Market和iOS应用商店的应用?

iphone - 在 NSObject 中添加一个 @property 作为指向另一个 NSObject 中的@property 的指针

ios - 从 UISegmentedControl 获取字符串值

ios - 为什么 UITableViewDataSource 或 UITableViewDelegate 不需要在 UITableView 中定义为弱?

ios - Swift 中的 UITableViewCell 中的 UIScrollView

ios - UITableViewController 和 UIViewController 的父类(super class)

ios - UISearchDisplayController 处于事件状态时搜索栏上方的额外空间

ios - 如何使用按钮显示 "Search Display Controller"

ios - 有谁知道如何实现在表格 View 单元格的搜索栏中显示电话簿中的联系人列表?