Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Layout not working?

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.

like image 319
fumoboy007 Avatar asked Feb 26 '13 19:02

fumoboy007


1 Answers

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

like image 101
Rob Avatar answered Oct 10 '22 21:10

Rob