iphone - 如何使用 iPhone SDK 3.0 中的 UISearchDisplayController 实现始终位于顶部的搜索栏

标签 iphone iphone-sdk-3.0 uisearchbar searchdisplaycontroller

iPhone SDK 3.0 有这个方便的新类“UISearchDisplayController”,可以轻松创建搜索栏、处理所有用户输入并显示搜索结果。

我正在将它用于我的新应用程序,但我想更改一件事:

默认情况下,搜索栏应放置在显示搜索结果的 UITableView 的顶部。因此,当向下滚动结果列表时,搜索栏就会消失。

我想要实现的是让搜索栏始终位于顶部,并且在滚动 TableView 时,搜索栏应保持在原来的位置。 (原因:在我的应用程序中有时会有很多搜索结果,所以有时用户必须向下滚动一段时间,当他意识到必须更改搜索字符串时,我不想强​​迫他一直向后滚动)

我已经做的是将搜索栏添加到 UINavigationController 的 View 中,这是我的表格 View 的“父 View ”,而不是直接将其添加到表格 View 中:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;

[searchBar sizeToFit];
CGFloat searchBarHeight = [searchBar frame].size.height;
CGRect mainViewBounds = delegate.navController.view.bounds;
[searchBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
                             CGRectGetMinY(mainViewBounds) + 20,
                             CGRectGetWidth(mainViewBounds),
                             searchBarHeight)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[delegate.navController.view addSubview: searchBar];

searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar: searchBar contentsController: self];
searchDisplayController.searchResultsDataSource = self;
searchDisplayController.searchResultsDelegate = self;

...但我的问题是,表格 View 位于搜索栏后面,并且搜索结果的第一个 TableViewCell 始终隐藏在我的搜索栏后面。

有没有办法永久调整 UITableViewController 的 UITableView 大小,以便它在搜索栏下方开始?

还有另一个问题:每次我触摸搜索栏时,UISearchDisplayController 都会自动隐藏导航栏并调整 UITableView 的大小(它扩展到顶部),所以我认为此时 UISearchDisplayController 会对我的应用程序产生很大的问题。自定义大小的 TableView...

非常感谢您的帮助!!

最佳答案

我的脑海中没有解决方案 - 但我会说,通过用户只需点击屏幕顶部并将表格缩放到顶部的能力,您的担忧似乎最小化了(如果搜索词有误,请更改)。

关于iphone - 如何使用 iPhone SDK 3.0 中的 UISearchDisplayController 实现始终位于顶部的搜索栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1213717/

相关文章:

iphone - MapKit 用户位置(蓝点和圆圈)将不会显示

iphone - iPhone 应用程序本地化 - 您的方法是什么?

iOS6, Storyboard : UISearchDisplayController returns wrong custom UICell height

objective-c - 使用 NSPredicate 过滤时返回非常困惑的结果

iphone - FFmpeg 不解码 h264 流

iPhone 4S - 它会有 UDID 吗?

ios - 修改正在运行的 SKAction 的速度

iphone - 将记录保存到 coreData 需要很长时间,我怎样才能更快地插入它们?

iphone - 如何在 NIB 中加载外部 View ?