Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect tapping on the gray (dimmed) background in UISearchDisplayController

I am working on a iOS project where I want to implement a search functionality. I'm using UISearchBar and UISearchDisplayController and I would like to make UISearchBar implicitly hidden. When user taps on the UIBarButtonItem, the search bar will appear and "becomeFirstResponder", whereas when user clicks on the Cancel button in the search bar, it should disappear.

The appearing is working correctly, but I have a problem with disappearing. I managed to hide the search bar when user taps on the Cancel button (searchBarCancelButtonClicked method), but I'm unable to hide the search bar when the grey (dimmed) background in UISearchDisplayController is tapped. As far as I know, there is no method to detect this event, so it is necessary to apply some hack. Do you know any, or what is the best way to detect tapping on the gray background?

like image 430
zvonicek Avatar asked Jun 01 '12 18:06

zvonicek


1 Answers

So I've finally worked it out. Apart from the searchBarCancelButtonClicked:(UISearchBar *)searchBar, it is also necessary to hide the search bar in searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller. It looks this callback is called even when the gray (dimmed) background is tapped, so it can be easily used for hiding the search bar.

like image 154
zvonicek Avatar answered Nov 04 '22 11:11

zvonicek