Scroll UITableView so that the header isn't visible

Jens Kohl picture Jens Kohl · Jul 3, 2009 · Viewed 20.8k times · Source

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?

Answer

Joe picture Joe · Jul 7, 2009

Maybe you can try it this way...

[self.tableView setContentOffset:CGPointMake(0,40)];