I have a UITableViewController with a UISearchDisplayController setup in the standard way (with the search bar inside the tableView). I'd like the search bar to start out hidden - really hidden, not merely scrolled away as in this solution. Then I'd like to present the search UI when the user presses a button, and hide it again (really hide it) after the user selects one of the items found in the search.
Here's the almost working code for that:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.searchDisplayController.searchBar.prompt = @"Add an item";
self.searchDisplayController.searchBar.placeholder = @"Type the item name";
// i do this to trigger the formatting logic below
[self.searchDisplayController setActive:YES animated:NO];
[self.searchDisplayController setActive:NO animated:NO];
// rest of my view will appear
}
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller {
NSTimeInterval duration = (self.isViewLoaded && self.view.window)? 0.3 : 0.0;
__block CGFloat searchBarHeight = controller.searchBar.frame.size.height;
[UIView animateWithDuration:duration animations:^{
self.tableView.contentOffset = CGPointMake(0, searchBarHeight);
self.tableView.contentInset = UIEdgeInsetsMake(-searchBarHeight, 0, 0, 0); // trouble here, I think
} completion:^(BOOL finished) {
controller.searchBar.hidden = YES;
}];
}
to show
- (IBAction)pressedAddButton:(id)sender {
self.searchDisplayController.searchBar.hidden = NO;
[self.searchDisplayController setActive:YES animated:YES];
}
I think I'm properly setting the content inset, but it behaves unexpectedly: with the code as shown, the table allows the content move up too far, i.e. with only two not-very-tall rows, I'm able to scroll down, pushing most of those two rows above the top of the table...like there's no bottom bounce. When I comment out the contentInset line, the table lets me pull the content down too far, leaving a big gap above row 0, presumably where the search bar is sitting hidden.
Much obliged if anyone can help.