Reputation: 38162
I have a table view with a search bar on top of it. My requirement is to do not show the search bar when someone open the page but when someone slides the table down then the search bar should be visible.
Upvotes: 12
Views: 10965
Reputation: 2304
Related to murat's answer, here's a more portable and correct version that will do away with animated offsetting on view load (it assumes the search bar has an outlet property called searchBar
):
- (void)viewWillAppear:(BOOL)animated
{
self.tableView.contentOffset = CGPointMake(0, self.searchBar.frame.size.height);
}
UPDATE:
To accommodate tapping on the search icon in the section index, the following method needs to be implemented, which restores the content offset:
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index
{
index--;
if (index < 0) {
[tableView
setContentOffset:CGPointMake(0.0, -tableView.contentInset.top)];
return NSNotFound;
}
return index;
}
Upvotes: 4
Reputation: 4963
In your controller's viewDidAppear:
method, set the contentOffset property (in UIScrollView) of your table view to hide the search bar.
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
self.tableView.contentOffset = CGPointMake(0, SEARCH_BAR_HEIGHT);
}
Upvotes: 23