9

I'm adding search bar on table header and floating it in scrollViewDidScroll method, but when i scroll without click on search bar(i.e. i go to the view and do scroll) then search bar doesn't stay on top but it scroll up with table however once i click on search bar and click cancel button on search bar and then if i scroll the table, search bar stays on top.here is my code-

-(void)viewDidLoad {
    [super viewDidLoad];

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    searchBar.delegate = self;

    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.delegate = self;
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;

    UIView *tableHeaderView = [[UIView alloc] initWithFrame:searchDisplayController.searchBar.frame];
    [tableHeaderView addSubview:searchDisplayController.searchBar];
    [tableView setTableHeaderView:tableHeaderView];

    isSearching = NO;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {

    UISearchBar *searchBar = searchDisplayController.searchBar;
    CGRect searchBarFrame = searchBar.frame;

    if (isSearching) {
        searchBarFrame.origin.y = 0;
    } else {
        searchBarFrame.origin.y = MAX(0, scrollView.contentOffset.y + scrollView.contentInset.top);
    }

    searchDisplayController.searchBar.frame = searchBarFrame;
}

- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller {
    isSearching = YES;
}

-(void)searchDisplayControllerWillEndSearch:(UISearchDisplayController *)controller {
    isSearching = NO;
}

Note that I'm using UITableViewController sub class and don't want to change it to UIViewController. Any help would be appreciated.

Edit: I also using section header in this UITableViewController, in other UITableViewController there is no section header and this code working fine.Is this a problem with section header and table header together?

Bharat
  • 2,987
  • 2
  • 32
  • 48
  • 1
    you should try add it to the section header – Bogdan Somlea Jul 17 '14 at 06:34
  • the tableHeader goes off the screen when you are scrolling, the section header is leaving the screen only when you start scrolling, and you are getting to the next section. If you have only 1 section the best way is to add the searchBar inside the section Header, otherwise you can't do it inside of the UItableViewController – Bogdan Somlea Jul 17 '14 at 06:57
  • Ok..i did that but now if i click on search bar, app crash without any error? – Bharat Jul 17 '14 at 07:17
  • there is allways an error, just find it and show it to me – Bogdan Somlea Jul 17 '14 at 07:21
  • If you consider my answer as correct, please mark it as so. – Benjamin Dec 19 '14 at 14:50

2 Answers2

18

The reason why your searchbar is scrolling with the table contents is that you have put it directly IN the table, thus making it a child header section of the table. and that section ALWAYS scrolls…

Here is how this this can be achieved. And it is actually quite simple. (The following example relies on Storyboard, but the mechanism is the same whatever you are using) :

1) Use a UIVIewController and NOT a UITableViewController

2) Add a UITableView as the child of the parent UIView

3) Add a UISearchBarController also as a child view of the UIView, NOT as a child of the UITableView (UITableView and UISearchController are siblings)

you should have the following layout :

enter image description here

EDIT : The important thing to remember is to put the UISearchBarController ABOVE the sibling UITableView. Otherwise you may see the UITableView overlap the UISearchBarController when the latter is focused.

EDIT 2 : BTW, if you are using AutoLayout, remember to set the TOP constraint of the tableView relative to the SearchBar…

Run it and admire the result.

Hope this helps.

Benjamin
  • 8,128
  • 3
  • 34
  • 45
3

There is not a way to maintain the header of a tableView fixed

1- could use an UIViewController instead of UITableViewController.

2- add subview (UIView) for header.

3- and add another subview for the tableview.

Mohamad Chami
  • 1,226
  • 14
  • 10