Usually you just use UISearchBar
as a tableViewHeader
for your table view. If you want it hidden when the user enters the screen (like it's done in most native apps) you can just set contentOffset
for the tableView
in viewWillAppear
.
And I am pretty sure that's in fact how they do it. And if you think about it it's what tableHeaderView
is meant for.
Try something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0., 0., 320., 44.)];
self.tableView.tableHeaderView = searchBar;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGPoint contentOffset = self.tableView.contentOffset;
contentOffset.y += CGRectGetHeight(self.tableView.tableHeaderView.frame);
self.tableView.contentOffset = contentOffset;
}
Note that in iOS 7 you should not just set the contentOffset
of your tableView
to CGPointMake(0., CGRectGetHeight(self.tableView.tableHeaderView.frame))
if your viewController
has automaticallyAdjustsScrollViewInsets
set to YES
, since it probably will not be CGPointZero
in viewWillAppear: