Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController's search bar overlaps first tableview cell

I am using UISearchController and the UISearchBar also has a scope bar. The search bar is added as a subview to a UIView which is above the UItableView. I have done it this way since I want the search bar to be always visible even when the tableview is scrolled.

The problem is the scopebar overlaps the first tableview cell

StoryBoard

enter image description here

Scope bar overlapping the tableview cell

enter image description here

How can I prevent this overlapping?, I can't display the searchbar in navigationbar since the scopebar when placed in navigation bar does not get displayed.

like image 873
Praveen Gowda I V Avatar asked Mar 25 '15 19:03

Praveen Gowda I V


2 Answers

This worked for me:

Having the Search Display Controller and SearchBar in the tableview header. Add the heightForHeaderInSection in your TableViewController.

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44; //I used the 44, the height of SearchBar
}

In your case you'll need to also add the scope bar height. Now it will always keep a base height.

like image 51
MontiRabbit Avatar answered Nov 12 '22 15:11

MontiRabbit


This worked for me :

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.searchController.active) {
        return 44; // with scope
    } else {
        return 0; // no scope
    }
}
like image 32
Web Dream Avatar answered Nov 12 '22 15:11

Web Dream