UISearchBar subtitle UITableViewHeader?

I want to add a UISearchBar to a UITableView that already has a title. When I try to add a search bar to an existing title, it works until I touch it, after which I get The view hierarchy is not prepared for the constraint , which looks like the search bar is not a direct view of the table, so when UISearchController is trying to update restrictions that it cannot.

The only way I found is to make the table title a title in the search bar, then everything will be fine, but, of course, then I will lose the other views that were already in the title view.

+6
source share
1 answer

To get around this behavior, I placed my search bar in a UIView container. Apply restrictions to this container view and use the auto-size mask for the search string in the container.

 // Configure header view UIView *headerView = ... ... // Create container view for search bar UIView *searchBarContainer = [UIView new]; searchBarContainer.translatesAutoresizingMaskIntoConstraints = NO; [searchBarContainer addSubview:self.searchBar]; [headerView addSubview:searchBarContainer]; self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; // Apply constraints involving searchBarContainer [headerView addConstraint: ...]; ... // Then add header to table view self.tableView.tableHeaderView = headerView; 
+4
source

Source: https://habr.com/ru/post/979730/


All Articles