【问题标题】:iPhone SDK: Setting the size of UISearchDisplayController's table viewiPhone SDK:设置 UISearchDisplayController 的表格视图的大小
【发布时间】:2010-03-05 18:06:31
【问题描述】:

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

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

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

编辑以添加屏幕截图:

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


(来源:lightwood.net

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

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


(来源:lightwood.net

【问题讨论】:

  • 另外值得注意的是,我尝试过不同高度的table view,搜索显示结果总是延伸到屏幕底部。问题不在于搜索栏本身约为 50 像素,将所有内容都推到横幅后面。

标签: iphone uitableview uisearchdisplaycontroller


【解决方案1】:

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

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

【讨论】:

  • 感谢这位朋友。这让我发疯了!
  • 如何声明someframe
  • @user2872856 tableView.frame 是一个 CGRect,所以你可以这样做:CGRect someframe = CGRectMake(CGFloat x, CGFloat y, CGFloat width, CGFloat height);
【解决方案2】:

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

-(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】:

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

    听起来好像是 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 像素,然后做一些数学运算来计算您的新中心。

    【讨论】:

    • 我添加了一些截图。我绝不会为搜索表显示设置几何图形。如果我能弄清楚在哪里执行此操作,那可能就是答案……但是 UISearchDisplayController 的 searchResultsTableView 属性是只读的。
    • searchResultsTableView 属性是只读的,但这只是意味着您不能更改控制器上的句柄,您应该能够操作 tableview 本身。
    • 有两个UITableView,一个是你自己创建的,一个是搜索结果控制器创建的。如果您希望它们都具有相同的效果,则需要同时操作它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多