UISearchDisplayController 奇怪的动画和偏移
Posted
技术标签:
【中文标题】UISearchDisplayController 奇怪的动画和偏移【英文标题】:UISearchDisplayController strange animation and offset 【发布时间】:2013-12-27 14:42:22 【问题描述】:我创建了一个UITableViewController
并以编程方式添加了一个UISearchDisplayController
。没有故事板或 nib 文件,因为我正在以编程方式进行所有操作。一切似乎都很完美,除了当我点击搜索栏时,它的动画很奇怪,而且搜索栏底部还有一些偏移。
Here's a video 的行为。
这是表 VC 的代码:
// Initialize Search Bar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
self.searchBar.delegate = self;
self.tableView.tableHeaderView = self.searchBar;
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsDelegate = self;
self.searchController.delegate = self;
// Hide the search bar
[self.tableView setContentOffset:CGPointMake(0, 44)];
我还在 UITableViewController 的实现上尝试了[self setEdgesForExtendedLayout:UIRectEdgeNone];
,但它产生了另一个问题,尽管它似乎在某种程度上修复了动画,在顶部留下了一些间隙,在搜索栏的底部留下了相同的间隙。
编辑:还有一点需要注意——在视频中可以看到结果表视图与搜索栏正确对齐。那么主表视图的实现有没有问题呢?它们是相同的,但前者正确对齐,所以我猜表格视图有问题。你怎么看?
【问题讨论】:
【参考方案1】:ios7 使以编程方式添加这些元素变得不必要地困难。在我自己的应用程序中进行了大量测试后,这是我在UITableViewController
中的操作方式。
// _searchBar
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, 44.0f)];
self.searchBar.delegate = self;
// _searchController
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:[self searchBar] contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsTableView.frame = [self.tableView frame];
// _tableView
self.tableView.tableHeaderView = [self.searchController searchBar];
不要担心隐藏搜索栏,因为当您点击取消时,操作系统会将所有内容重置为其原始状态。
【讨论】:
以上是关于UISearchDisplayController 奇怪的动画和偏移的主要内容,如果未能解决你的问题,请参考以下文章
为啥 UISearchDisplayController 有时有效,有时无效?
iOS UISearchDisplayController学习笔记
在 UISearchDisplayController 上遇到僵尸问题
修复 UITableView 顶部的 UISearchdisplaycontroller 搜索栏