13

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 ?

Ludovic Landry
  • 11,606
  • 10
  • 48
  • 80

4 Answers4

4

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.

Community
  • 1
  • 1
Walter
  • 5,867
  • 2
  • 30
  • 43
  • So we need to do everything "manually" there is no simple component do do that ? – Ludovic Landry Feb 11 '11 at 09:10
  • You could search around on github or bitbucket to see if someone has created a library you can borrow. Otherwise, I think that you have to take care of it with your own code. – Walter Feb 11 '11 at 19:09
1

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.

Jay
  • 856
  • 7
  • 17
  • 1. Yes I know and I don't want that scroll / 2. Because I want to hide the UIScrollBar when I'm scrolling to the bottom – Ludovic Landry Feb 10 '11 at 11:10
  • 1
    If you want to hide while scrolling downwards, then try using delegate scrollViewDidBeginScrolling, and inside the delegate if scrollView.contentOffset greater than zero make the searchBar hidden. – Jay Feb 11 '11 at 07:46
0

This will answer your question :

iPhone: Hide UITableView search bar by default

same concept, different controller.

Good luck.

Community
  • 1
  • 1
Ben
  • 953
  • 12
  • 27
0

The right side 's UISearchBar is on the another UIView, it is not a part of UITableView. So, you can add a child view to put the UISearchBar, and another child view is for the UITableView.

AechoLiu
  • 17,522
  • 9
  • 100
  • 118