1
votes

I'm working on implementing a basic searchbar progmatically. I can't figure out how to make the searchbar stick to the header as the table scrolls. here is my code for loading the searchbar :

-(void)loadbar{
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar         contentsController:self];
searchDisplayController.delegate = self;
searchDisplayController.searchResultsDataSource = self;
self.tableView.tableHeaderView = searchBar;
}

Search bar inherits from UISearchBar and searchDisplayController inherits from UISearchDisplayController

Thanks!

2
What do you mean by making it scroll? Do you want the search bar to scroll when the table view scrolls (which it will with your code) or do you mean something else?rmaddy
You told us what you want to do. You posted some code. But you have not told us what problem you are having. Please explain.rmaddy
@rmaddy please see update. I would like to be able to scroll the tableview and still have the search bar stuck on top.Blake
OK, then you want a search bar that doesn't scroll. Please update your title and your question to make this clear. If you don't want it to scroll then you can't make it part of the table view.rmaddy
@rmaddy updated my question. sorry about the confusion and thanks for the help.Blake

2 Answers

2
votes

A table's header view scrolls with the table. If you don't want the search bar to scroll with the table view, you can't set the search bar as the header view.

You have three options.

  1. Add the search bar to the navigation bar
  2. Make the search bar a subview of the tableview. Implement the scrollViewDidScroll: delegate method for the table view and adjust the position of the scroll bar as the table scrolls.
  3. Don't use UITableViewController. Use UIViewController and add your own table view. Add the search bar at the top and the table view below the search bar.
0
votes

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;
}