为什么 UISearchBar 及其范围按钮显示在一行中?
我见过苹果的示例“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];
}
提前致谢。
I have seen Apple's example "TableSearch" that when touched its scope buttons come below the search bar.
http://developer.apple.com/iphone/library/samplecode/TableSearch/Introduction/Intro.html
But when I make my own it looks good at first but when I touch it looks like ugly, scope buttons and search bar are shown in the same line like this:
http://cl.ly/BN9
What do I have to do make it like "TableSearch" example in the iPad?
I am doing everything in IB and tried to modify the search bar programatically from the controller:
- (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];
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也遇到了这个错误,我已经向 Apple 提交了报告并请求技术帮助。我会让你知道进展如何。与此同时,我将向您简要介绍此错误的背景。
在 iPhone 上,为了在横向模式下保留宝贵的垂直屏幕空间,UISearchDisplayController 将 UISearchBar 设置为将其搜索栏和搜索字段组合在单个水平布局中。由于屏幕的水平尺寸增加(横向为 480 点),因此效果非常好。不幸的是,它在 iPad 上的表现不太好,在横向模式下,UI 更改实际上并不是必需的,因为你有足够的垂直空间。 UISplitViewController 的主视图中仍然只有 320 像素的水平显示空间,而不是 iPhone 增加的 480 像素。结果是 iSore。
据推测,问题在于 UISearchDisplayController 在其 willRotateToInterfaceOrientation:duration: 方法中表现不佳。具体来说,在设置 UISearchBar 上的 mergesLandscapeBars 属性之前,它不会检查它是否在 iPhone 上。代码中的私有 API 组合之所以有效,是因为它修复了 UISearchDisplayController 中的疏忽。但苹果当然会因你使用未记录的 API 而将古人的愤怒倾注到你身上,所以你不能。确实,我们在这一点上受到了苹果的摆布。
如果您愿意放弃 UISearchDisplayController 的美观和便利,您可以使用没有 UISearchDisplayController 的 UISearchBar 并自行管理演示的各个方面。显然,这需要更多的代码,如果苹果的 API 工程师完成了他们的工作,这将毫无意义,但它至少会解决显示错误。
如果您是Apple,则可以使用自己的未记录的API,这就是Mail.app 不存在此问题的原因。
更新
我向 Apple 提交的错误报告是#8344719。
I've encountered this bug as well, and I've both filed a report with Apple and requested technical assistance. I'll let you know how it goes. In the meantime I'll give you a brief bit of background on this bug.
On the iPhone, to preserve precious vertical screen real estate in Landscape mode, the UISearchDisplayController sets the UISearchBar to combine its search bar and search field in a single horizontal layout. This works pretty well because of the increased horizontal size of the screen (480 points in Landscape). Unfortunately, it works not so well on the iPad, where in Landscape mode the UI change really isn't necessary in the first place because you have plenty of vertical real estate. You also still only have 320 pixels of horizontal display space in the master view of the UISplitViewController, not the increased 480 of the iPhone. The result is an iSore.
Presumably the problem is that UISearchDisplayController is behaving badly in its willRotateToInterfaceOrientation:duration: method. Specifically, it's not bothering to check whether it's on an iPhone or not before it sets the combinesLandscapeBars property on its UISearchBar. The private API kludge in your code works because it fixes that oversight in the UISearchDisplayController. But of course Apple will rain down the fury of the ancients on you for using undocumented APIs, so you can't. Really we're at the mercy of Apple on this one.
If you're willing to give up the eye-candy and convenience of UISearchDisplayController, you can use a UISearchBar sans UISearchDisplayController and manage aspects of presentation yourself. Obviously this requires a lot more code and would be pointless if Apple's API engineers did their jobs, but it will at least resolve the display bug.
If you're Apple you can use your own undocumented APIs, which is why Mail.app doesn't have this problem.
UPDATE
The bug report I've filed with Apple is #8344719.
使用以下代码,您将不会收到警告:
Using the following code, you will not get warning:
当您设置搜索栏的边界和框架时,就像此处的框架一样:
看来身高有问题。范围按钮需要搜索栏下方的一些空间,因此您应该增加边界和框架的高度。
如果您在某些事件上显示和隐藏范围按钮,则每次都需要调整帧大小。
When you set the bounds and frame for the searchBar, like for the frame here:
it seems like there is a problem with height. Scope buttons require some space under the search bar, so you should increase the height of both bounds and the frame.
If you show and hide scope buttons on some event you need to adjust the frame size each time.
我在 iPad 上遇到了这个问题,但如果我将其插入到我的实现文件中,我可以让它工作:
我猜正在加载的 XIB 有问题,但我不知道为什么这会解决它。
I'm getting this issue on an iPad, but I can get it to work if I insert this in my implementation file:
I'm guessing there is something bad with the XIB being loaded, but I don't know why this would fix it.