6

我在使用自动布局设置的表格视图顶部有一个搜索栏,如下所示:

_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_searchBar.showsScopeBar = YES;

4

1 回答 1

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];
}

查看UISearchBar 的范围按钮不会显示 iOS6

于 2013-02-26T20:40:33.333 回答