How to implement a search bar that stays always on top using UISearchDisplayController in iPhone SDK 3.0

xoconoxtle picture xoconoxtle · Jul 31, 2009 · Viewed 8.9k times · Source

The iPhone SDK 3.0 has this handy new class "UISearchDisplayController" which makes it easy to create a search bar, handling all the user input and displaying the search results.

I am using it for my new app, but there is one thing i would like to change:

As a default, the search bar should be put at the top of the UITableView that displays the search results. So when scrolling down the result list, the search bar disappears.

What I would like to achieve is having the search bar always on top and when scrolling the TableView, the search bar should stay where it is. (Reason for this: in my app there are sometimes a lot of search results, so sometimes the user has to scroll down a while, and when he realizes that he has to change his search string, i don't want to force him to scroll all the way back)

What i already did is adding the search bar to the view of the UINavigationController which is the "parent view" of my table view, instead of adding it to the table view directly:

MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];

searchBar = [[UISearchBar alloc] init];
searchBar.delegate = self;

[searchBar sizeToFit];
CGFloat searchBarHeight = [searchBar frame].size.height;
CGRect mainViewBounds = delegate.navController.view.bounds;
[searchBar setFrame:CGRectMake(CGRectGetMinX(mainViewBounds),
                             CGRectGetMinY(mainViewBounds) + 20,
                             CGRectGetWidth(mainViewBounds),
                             searchBarHeight)];
searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;

[delegate.navController.view addSubview: searchBar];

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

... but here my problem is, that the table view stays behind the search bar, and there is always the first TableViewCell of the search results, that is hidden behind my search bar.

Is there a way to resize the UITableView of my UITableViewController permanently, so that it starts right under the search bar?

And there's another problem: Everytime i touch the search bar, the UISearchDisplayController automatically hides the Navigation Bar and resizes the UITableView (it expands to the top), so i think that at this point the UISearchDisplayController will have a big problem with my custom sized TableView...

Thanks a lot for any help!!

Answer

Kendall Helmstetter Gelner picture Kendall Helmstetter Gelner · Jul 31, 2009

I don't have a solution off the top of my head - but I will say the concern you have seems to minimized by the ability for the user to simply tap the top of the screen and have the table zoom to the top (to change search terms if it's wrong).