15
votes

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.

3
you could try setting tableView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0) which will push tableView's content 64 points down… you should set that to what ever the size of the scopebar is...chuthan20
@chuthan20 Where exactly shall I try setting this, is there a method that is triggered when UISearchController becomes active?Praveen Gowda I V
You could set your view controller to be the delegate of UISearchController, https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchControllerDelegate_Ref/index.htmlchuthan20

3 Answers

6
votes

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.

3
votes

This worked for me :

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.searchController.active) {
        return 44; // with scope
    } else {
        return 0; // no scope
    }
}
0
votes

Add the search bar and table view on the UIViewController as shown in the image below. Don't overlap the search bar with the table view. I am using this in my app and its working fine for me.

enter image description here