我在使用自动布局设置的表格视图顶部有一个搜索栏,如下所示:
_searchBar.translatesAutoresizingMaskIntoConstraints = NO;
_tableView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_searchBar]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_searchBar)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_tableView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView)]];
[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_searchBar][_tableView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_searchBar, _tableView)]];
运行时一切都很好看。但是当我在开始编辑搜索栏之前_searchBar.showsScopeBar = YES;
时,搜索栏和表格视图不会自动调整大小。即使我执行[_searchBar sizeToFit]
,表视图也不会调整大小并向下移动。为什么?
注意:我没有把搜索栏作为表格视图的标题;它只是一个父视图和两个子视图。
注意2:在我致电intrinsicContentSize
之前和之后,我检查了_searchBar
的{{1}},并且尺寸确实发生了变化。
答案 0 :(得分:4)
您必须invalidateIntrinsicContentSize
:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
searchBar.showsScopeBar = YES;
[searchBar invalidateIntrinsicContentSize];
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsScopeBar = NO;
[searchBar invalidateIntrinsicContentSize];
[searchBar setShowsCancelButton:NO animated:YES];
}