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.


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.
ios iphone uitableview ios7 uisearchbar
hpique
source share