Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchBar on the top of UITableView which can hide but stay close to UINavigationBar

I would like to have a UISearchBar on the top of my UITableView which hides when you scroll down: The answer is easy, I just need to add it on my table view header like this:

UISearchBar *search = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
self.tableView.tableHeaderView = search;
[search release];

But the problem is that when you scroll up the UISearchBar fit to the top of the UITableView, and not the top of the UINavigationBar. To be more clear I made a screen in Mail (not good) and Game Center (good).

enter image description here

I want the same as in Game Center. Do you have any idea how they are doing this ?

like image 715
Ludovic Landry Avatar asked Feb 10 '11 10:02

Ludovic Landry


2 Answers

You will need to hide the search bar on your own when you scroll the tableview. So, don't put it as a UITableView header. You could hide it by setting its height to zero. That way if your tableview is set to autoresize it will expand.

I would experiment with having the UITableView and the UISearchBar as peers within another view. The GameCenter image does not have the search bar as the table view header, rather it has them as separate subviews.

You could also look at UISearchDisplayController but I think it doesn't quite have the behaviour that you want.

Edit: This question is basically your question and has some code in the answers.

like image 178
Walter Avatar answered Sep 28 '22 02:09

Walter


A tableview header will move while scrolling downwards. It is tableview's property. If you want to place a searchBar with navigation bar. Why dont you place the search bar just below the navigation bar. Use search bar and separate tableview.

like image 29
Jay Avatar answered Sep 28 '22 01:09

Jay