Does the machine not work?

I have a search bar on top of a table view configured using an automatic layout, for example:

_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)]]; 

Everything looks good when I run it. But when I do _searchBar.showsScopeBar = YES; Before you start editing the search bar, the search bar and table view do not change automatically. Even when I do [_searchBar sizeToFit] , the table view does not resize and does not move down. Why??

Note. I do not put a search string in the table title; it's just a parent view and two subheadings. Note 2: I checked intrinsicContentSize in _searchBar before and after calling _searchBar.showsScopeBar = YES; , and the size has really changed.

+6
source share
1 answer

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

See Scope Button UISearchBar Does Not Display iOS6

+4
source

All Articles