UISearchController's search bar overlaps first tableview cell
Asked Answered
G

3

15

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.

Gemma answered 25/3, 2015 at 19:16 Comment(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...Unshroud
@Unshroud Where exactly shall I try setting this, is there a method that is triggered when UISearchController becomes active?Gemma
You could set your view controller to be the delegate of UISearchController, https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISearchControllerDelegate_Ref/index.htmlUnshroud
N
6

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.

Northeaster answered 7/10, 2015 at 15:50 Comment(0)
V
3

This worked for me :

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (self.searchController.active) {
        return 44; // with scope
    } else {
        return 0; // no scope
    }
}
Vishinsky answered 22/8, 2017 at 14:0 Comment(1)
Works for me! For table with sections, when search controller is not active I changed to "return 20; // no scope". Because when is 0, this hides the sections headers.Rebirth
C
0

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

Calcic answered 17/6, 2015 at 6:37 Comment(1)
I was able to get it to work by using UIEdgeInsetsMake as suggested in the comments. I have setup exactly like you have done in your image, but that doesn't workGemma

© 2022 - 2024 — McMap. All rights reserved.