Why is this UISearchBar changing?

As shown below, my UISearchBar changes when I click on the search box. It animates nicely to cover the navigation bar, and then pop ... it shrinks.

Hcspp.pngKlvTE.png

Customization

UISearchBar is inside the vanilla UIView set as tableHeaderView . I use UIView (as opposed to setting the UISearchBar as the header) because I would like to add additional headers to the header.

The view is defined in the XIB file, and the UISearchBar bound to all its boundaries. Limitations do not seem important; if I delete them, the same problem arises.

The experiments

Studying the view hierarchy in Reveal tells me that the UIView is the right size and the UISearchBar is 1 (?!) UIView when this happens .

As an experiment, I subclassed a UISearchBar and made an intrinsicContentSize return {320,44} . In doing so, the UISearchBar displays correctly, but when I click Cancel, I get the following exception:

  *** Assertion failure in -[UITableView layoutSublayersOfLayer:], /SourceCache/UIKit_Sim/UIKit-2903.23/UIView.m:8540 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. UITableView implementation of -layoutSubviews needs to call super.' 

Workaround

If I create everything by code, it just works.

 _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)]; searchBar.delegate = self; [_headerView addSubview:searchBar]; _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self]; _searchDisplayController.searchResultsDelegate = self; _searchDisplayController.searchResultsDataSource = self; _searchDisplayController.delegate = self; _tableView.tableViewHeader = _headerView; 

What's going on here? I obviously won’t use nibs to work with UISearchBar anymore, but I would like to understand what happened after all.

+7
ios iphone uitableview ios7 uisearchbar
source share
2 answers

The difference lies in the value of the translatesAutoresizingMaskIntoConstraints search bar. The default value is NO for XIB-based representation and YES for code representation. Apparently, the search controller takes the value YES and does not impose any explicit restrictions when transplanting the search string.

Setting the value to YES in the code should fix it (checked locally).

UPDATE

As noted in the comments, even with translatesAutoresizingMaskIntoConstraints = YES , the controller does not restore the header to its original state when canceled. But this seems to be a workaround. You can create an output in your title and restore it yourself in searchDisplayControllerDidEndSearch . I made an irrefutable proof of the 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]; } 
+14
source share

After both @TimothyMoose and @hpique said setting translatesAutoresizingMaskIntoConstraints = YES (yuck)

In my code, I found that if I do the following to show searchDisplayController

 self.searchDisplayController.searchBar.translatesAutoresizingMaskIntoConstraints = YES [self.searchDisplayController setActive:YES animated:YES] 

and do the opposite when closing searchDisplayController,

 self.searchDisplayController.searchBar.translatesAutoresizingMaskIntoConstraints = NO [self.searchDisplayController setActive:NO animated:YES] 

Then my look remains the same (everything goes back to what I expect)

if i don't call the next line when closing

 self.searchDisplayController.searchBar.translatesAutoresizingMaskIntoConstraints = NO 

Then the screw opens.

The reason for this is that I believe that NSAutoresizingMaskLayoutConstraint asserts itself and, since they are the last in the list of constraints, the Layout mechanism slows down the real constraint to satisfy the unwanted constraints of NSAutoresizingMaskLayoutConstraint.

+1
source share

All Articles