7

我已经看到 Apple 的示例“TableSearch”,当触摸它的范围按钮时,它位于搜索栏下方。 http://developer.apple.com/iphone/library/samplecode/TableSearch/Introduction/Intro.html

但是当我自己制作时,它起初看起来不错,但当我触摸它时看起来很丑,范围按钮和搜索栏显示在同一行,如下所示: http ://cl.ly/BN9

我该怎么做才能使它像 iPad 中的“TableSearch”示例?

我在 IB 中做所有事情,并尝试从控制器以编程方式修改搜索栏:

    - (void)viewDidLoad {

        [super viewDidLoad];
        self.tableView.rowHeight = 88.0f;
        self.tableView.contentOffset = CGPointMake(0, self.searchDisplayController.searchBar.frame.size.height);
self.searchDisplayController.searchResultsTableView.rowHeight = self.tableView.rowHeight;


    //BELOW DID NOT WORK:
    CGRect b = self.searchDisplayController.searchBar.bounds;
    self.searchDisplayController.searchBar.bounds = CGRectMake(b.origin.x, b.origin.y, b.size.width, self.tableView.rowHeight);
    b = self.searchDisplayController.searchBar.frame;
    self.searchDisplayController.searchBar.frame = CGRectMake(b.origin.x, b.origin.y, b.size.width, self.tableView.rowHeight);

    //BELOW WORKS PERFECT BUT IS A PRIVATE METHOD, HENCE I AM NOT SUPPOSED TO USE IT
    //[self.searchDisplayController.searchBar setCombinesLandscapeBars:NO];

     }

提前致谢。

4

4 回答 4

10

我也遇到了这个错误,我都向 Apple 提交了报告并请求技术帮助。我会让你知道情况如何。同时,我将简要介绍一下有关此错误的背景信息。

在 iPhone 上,为了在横向模式下保留宝贵的垂直屏幕空间,UISearchDisplayController 将 UISearchBar 设置为将其搜索栏和搜索字段组合在一个水平布局中。由于增加了屏幕的水平尺寸(横向 480 点),这非常有效。不幸的是,它在 iPad 上运行得不是很好,在横向模式下,UI 的改变一开始就没有必要,因为你有很多垂直空间。在 UISplitViewController 的主视图中,您仍然只有 320 像素的水平显示空间,而不是 iPhone 增加的 480 像素。结果是 iSore。

大概问题是 UISearchDisplayController 在其 willRotateToInterfaceOrientation:duration‎: 方法中表现不佳。具体来说,在其 UISearchBar 上设置 combineLandscapeBars 属性之前,无需检查它是否在 iPhone 上。代码中的私有 API 组件有效,因为它修复了 UISearchDisplayController 中的疏忽。但当然,Apple 会因为使用未记录的 API 而让古人对你大发雷霆,所以你不能。真的,我们在这个问题上受苹果的摆布。

如果您愿意放弃 UISearchDisplayController 的美观和便利,您可以使用没有 UISearchDisplayController 的 UISearchBar 并自己管理演示的各个方面。显然,这需要更多的代码,如果 Apple 的 API 工程师完成他们的工作,这将毫无意义,但它至少可以解决显示错误。

如果您是 Apple,则可以使用自己的未记录 API,这就是 Mail.app 没有此问题的原因。

更新

我向 Apple 提交的错误报告是 #8344719。

于 2010-08-24T01:11:40.607 回答
1

使用以下代码,您将不会收到警告:

if ([self.searchDisplayController.searchBar respondsToSelector:@selector(setCombinesLandscapeBars:)])
{
    objc_msgSend(self.searchDisplayController.searchBar, @selector(setCombinesLandscapeBars:), NO );
}
于 2011-11-14T13:12:46.587 回答
0

当您为 searchBar 设置边界和框架时,就像这里的框架一样:

blabla.searchBar.frame = CGRectMake(b.origin.x, 
                                    b.origin.y, 
                                    b.size.width, 
                                    self.tableView.rowHeight);

好像身高有问题。范围按钮在搜索栏下方需要一些空间,因此您应该增加边界和框架的高度。

如果您在某些事件上显示和隐藏范围按钮,则每次都需要调整帧大小。

于 2010-08-24T01:23:52.417 回答
0

我在 iPad 上遇到了这个问题,但如果我将它插入到我的实现文件中,我可以让它工作:

@synthesize tableView;

我猜想加载 XIB 有问题,但我不知道为什么这会解决它。

于 2010-12-13T21:24:03.990 回答