【发布时间】:2014-02-20 14:26:07
【问题描述】:
如下所示,当我点击搜索字段时,我的UISearchBar 正在调整大小。它的动画很好地覆盖了导航栏,然后弹出……它缩小了。
设置
UISearchBar 位于原版UIView 中,设置为tableHeaderView。我正在使用UIView(而不是将UISearchBar 设置为标题),因为我想在标题中添加其他视图。
视图在 XIB 文件中定义,UISearchBar 锚定到其所有边框。约束似乎无关紧要。如果我删除它们,会发生同样的问题。
实验
在 Reveal 中检查视图层次结构告诉我,UIView 的大小正确,UISearchBar 的宽度为 1 (?!) 当这种情况发生时。
作为一个实验,我将UISearchBar 子类化并让intrinsicContentSize 返回{320,44}。有了这个UISearchBar 可以正确显示,但是当我按下取消时,我得到了以下异常:
*** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2903.23/UIView.m:8540
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView's implementation of -layoutSubviews needs to call super.'
解决方法
如果我通过代码创建所有内容,它就可以工作。
_headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
searchBar.delegate = self;
[_headerView addSubview:searchBar];
_searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
_searchDisplayController.searchResultsDelegate = self;
_searchDisplayController.searchResultsDataSource = self;
_searchDisplayController.delegate = self;
_tableView.tableViewHeader = _headerView;
这里发生了什么?我显然不会再使用 nib 来处理 UISearchBar 了,但我还是想了解到底发生了什么。
【问题讨论】:
-
看起来可能有一个自动布局约束来将取消按钮和搜索字段设置为相同的大小。
-
@nhgrif 如果有,我肯定没加。
-
这就是为什么我将所有内容都放在代码中......
标签: ios iphone uitableview ios7 uisearchbar