I have a view which contains a UISearchBar. This UISearchBar is not displayed at the top of the page - it is about 90 pts down from the top of the screen, under a header image.
All elements of the view are wrapped in a UIScrollView. The right thing happens when the user taps in the UISearchBar - the entire view is scrolled up so the search bar is on the very top of the screen, a dark overlay appears over the view contents, and the keyboard slides up.
All good so far. The problem comes when I want to cancel the search, and have all the content return to it's previous location (with the UISearchBar no longer at the top of the screen).
I am able to do this if the user hits cancel while the keyboard is showing - simply listen for UIKeyboardWillHideNotification, then set the scroll view's content offset back to (0, 0).
However, if the user drags their finger across some search results, the keyboard will go away so you can get a larger view of the search results. If the user then hits cancel, the search bar remains at the top of the page, and everything looks terrible.
I tried putting the 'setContentOffset' code in the UISearchDisplayDelegate:
- (void)searchDisplayController:(UISearchDisplayController *)controller didHideSearchResultsTableView:(UITableView *)tableView
{
[_scrollView setContentOffset:CGPointMake(0,0) animated:YES];
}
This works, but doesn't look very good, because the scroll doesn't happen until AFTER the search results are faded away (so you see two separate animations).
I then tried this method (which is called right before the table view is hidden):
- (void)searchDisplayController:(UISearchDisplayController *)controller willHideSearchResultsTableView:(UITableView *)tableView
{
[_scrollView setContentOffset:CGPointMake(0,0) animated:YES];
}
However, that has no effect. The content offset is not changed.
Any tips?