Alex Rivera | Logout

Scroll UITableView so that the header isn't visible

Asked 2009-07-03T17:33:11.000
25

I've got a UITableView with a UISearchBar as the tableViews.tableHeaderView. Just like the new Mail.app, Notes.app, etc. in 3.0. I want to hide the SearchBar until the user drags it in his sight.

My attempt only works when there're a couple of items in the tableView, so that the tableView actually wants to scroll. I call this in loadView:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self._tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];

Nevertheless it seems that Apple handles such a serachbar differently. After draging out the searchbar it doesn't seem to be bounded to the tablecells anymore (in Notes.app, not in Mail.app).

But perhaps Apple has a distinct method for that new 3.0 behaviour, and I just can't find it?

Edit
Report

1 Answer

10

This one gets you the exact same behavior as iPod.app:

- (void)viewWillAppear:(BOOL)animated
{
 [super viewWillAppear:animated];

 CGFloat searchBarHeight = CGRectGetHeight([[[self searchDisplayController] searchBar] frame]);
 if ([[self tableView] contentOffset].y < searchBarHeight)
  [[self tableView] setContentOffset:CGPointMake(0, searchBarHeight)];
}
answered 2010-11-04T14:04:11.113

Your Answer