I have a search bar on top of a table view set up using auto layout like so:
_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 nice when I run it. But when I do _searchBar.showsScopeBar = YES;
before I start editing the search bar, the search bar and table view do not resize automatically. Even when I do [_searchBar sizeToFit]
, the table view does not resize and move down. Why??
Note: I'm not putting the search bar as the table view's header; it's just a parent view and two subviews.
Note 2: I checked the intrinsicContentSize
of _searchBar
before and after I call _searchBar.showsScopeBar = YES;
and the size does indeed change.
You have to 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 UISearchBar's scope button won't show up iOS6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With