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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With