As shown below, my UISearchBar
is getting resized when I tap on the search field. It animates nicely to cover the navigation bar, and then pop... it shrinks.
The difference is in the search bar's value of translatesAutoresizingMaskIntoConstraints
. It defaults to NO
for the XIB-based view and YES
for the code-based view. Apparently, the search controller assumes a value of YES and doesn't set up any explicit constraints when it transplants the search bar.
Setting the value to YES
in code should fix it (verified locally).
UPDATE
As noted in the comments, even with translatesAutoresizingMaskIntoConstraints = YES
, the controller does not restore the header view to it's original state on cancel. But there does seem to be a workaround. You can create an outlet to your header view and restore it yourself in searchDisplayControllerDidEndSearch
. I did a crude proof-of-concept (and updated my sample project):
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
self.tableView.tableHeaderView = self.headerView;
[self.searchBar removeFromSuperview];
self.searchBar.frame = CGRectMake(0, 20, 320, 44);
[self.headerView addSubview:self.searchBar];
}