IOS fixed search bar on top of UITableViewController?

I add a search bar to the table title and float it in the scrollViewDidScroll method, but when I scroll without clicking on the search bar (i.e. I go to the view and scroll), the search bar does not stay on top but it scrolls along with the table, but as soon as I click on the search bar and press the cancel button on the search bar, and then if I scroll through the table, the search bar remains 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; } 

Please note that I am using a subclass of UITableViewController and do not want to change it to UIViewController . Any help would be appreciated.

Edit: I also use the section header in this UITableViewController , in another UITableViewController there is no section header, and this code works fine. This is a problem with the section heading and the table heading together

+7
ios objective-c uisearchbar
source share
2 answers

The reason your search bar scrolls with the contents of the table is because you placed it directly in the table, thereby making it a child of the table header. and this section ALWAYS scrolls ...

Here's how to achieve this. And it is actually quite simple. (The following example is based on a Storyboard, but the mechanism is the same that you use):

1) Use UIVIewController and NOT UITableViewController

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

3) Add the UISearchBarController also as a child 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: It is important to remember that the UISearchBarController ABOVE is a UITableView. Otherwise, you can see the UITableView overlap of the UISearchBarController when the latter is focused .

EDIT 2: BTW, if you use AutoLayout, be sure to set the TOP restriction of the View table relative to the SearchBar ...

Run it and enjoy the result.

Hope this helps.

+15
source share

Unable to support fixed tableView header

1- can use UIViewController instead of UITableViewController.

2- add subview (UIView) for the title.

3- and add another view to represent the table.

+2
source share

All Articles