I have a problem: I have a UIView
that contains a UISearchBar
as a header of a UITableView
. The problem is that whenever I tap on the UISearchBar
, the animated scope buttons come out hiding part of the first row of the table. I looked at a lot of similar questions but none of the answers worked. Is there a way to add a bottom constraint to the headerView
so that the UITableView
goes down of the height of the scope buttons view whenever the UISearchBar
is focused and being edited?
This is the code where I add the UISearchController
:
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
definesPresentationContext = true
searchController.searchBar.scopeButtonTitles = ["Groups", "People"]
tableView.tableHeaderView = searchController.searchBar
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
searchController.searchBar.barTintColor = darkGreen
searchController.searchBar.tintColor = yellow
searchController.searchBar.backgroundColor = darkGreen
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
If you could please let me know how could I fix this bug I would really appreciate it!
Thanks!
This is happening because the HeaderView height is set to the Searchbar height which is 44 initially. But once you click on Searchbar the Searchbar is presented modally and moves to the top along with Scope buttons but the underlying HeaderView height is still the same which is 44 instead of 88 (Searchbar height + Scope button height)
For this how I have handled is -
1) Implement - UISearchControllerDelegate
and assign using searchController.delegate = self;
2) Override UISearchControllerDelegate methods -
-(void) didPresentSearchController:(UISearchController *)searchController {
[self fnResizeTableViewHeaderHeight];
}
-(void) didDismissSearchController:(UISearchController *)searchController {
[self fnResizeTableViewHeaderHeight];
}
3) Resize TableView header height depending on the search bar height -
-(void) fnResizeTableViewHeaderHeight {
CGFloat height = [searchController.searchBar systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
UIView *headerView = self.table_view.tableHeaderView;
CGRect frame = headerView.frame;
frame.size.height = height;
headerView.frame = frame;
self.table_view.tableHeaderView = headerView;
}
Set
searchController.hidesNavigationBarDuringPresentation = false
definesPresentationContext = false
This worked for me
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With