Solution
Use a one-section table view and set the search bar as the section header.
Discussion
Before, iOS was able to recognize when a search bar was in the table view header. In that case, the search bar was hidden under the navigation bar and it was possible to scroll down to reveal it. When in use, the search bar was fixed at the top of the window, not scrolling with the table view content. Since recently, this behavior is broken: the search bar scrolls with the cells.
In the proposed solution, we set the search bar as a section header. A section header remains visible until we scroll passed the end of that section. Thus, if we only have one section, the search bar is always visible.
Objective-C// Class members
UISearchBar *searchBar;
- (void)viewDidLoad {
// Inherited
[super viewDidLoad];
// Search Bar
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchBar.delegate = self;
searchBar.barStyle = UISearchBarStyleMinimal;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// It is mandatory to have one section, otherwise the search bar will scroll
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// TODO: Return the number of elements in the table
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return searchBar.frame.size.height;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
return searchBar;
}