Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar scope buttons in UITableView Header hide the first row of the table view

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!

like image 877
Giulio Colleluori Avatar asked Oct 16 '25 09:10

Giulio Colleluori


2 Answers

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;
}
like image 64
Fenil Avatar answered Oct 19 '25 00:10

Fenil


Set

searchController.hidesNavigationBarDuringPresentation = false
definesPresentationContext = false

This worked for me

like image 39
cwilliamsz Avatar answered Oct 19 '25 02:10

cwilliamsz