views:

28

answers:

1

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?

A: 

Perhaps the search display controller is setting the content offset for you? Try setting the content offset in searchDisplayController:didShowSearchResultsTableView: instead.

tc.
The search display controller is certainly setting the content offset when the search bar first gets focus. The problem I'm having is returning the scroll view offset back to 0, 0 when the user taps cancel AND the keyboard is already hidden. searchDisplayController:didShowSearchResultsTableView: is never called when the user taps cancel.
MikeQ
If you don't mind something slightly underhanded, you can use something like `-(BOOL)respondsToSelector:(SEL)selector { NSLog(@"%@",NSStringFromSelector(selector)); return [super respondsToSelector:selector];}` to see what other methods UISearchDisplayController uses, and override them to see if they have any effect.
tc.