9

I was wondering if its possible to activate a UISearchDisplayController from a button in the navigation bar rather than from the standard search bar that you get when you pull out the Search Bar Controller from there object library in Xcode. Like this functionality in the calendar app:

UISearchDisplayController in the Calendar app You can get a button to fire an action like so:

[self.searchDisplayController setActive:YES animated:YES];

But you still need the search bar and it animates from where the search bar is. Ideally I'd like it coming in from the top like in the gif.

Any help much appreciated. David.

lateAtNight
  • 482
  • 4
  • 13
  • Just create a search bar. Set the frame to be offscreen on load. Make sure it had an outlet property. Animate it down when the button is clicked and make the firstResponder – soulshined Jan 02 '15 at 18:57

1 Answers1

9

Old question, but I have an answer so I thought it was worth revisiting...

In the end I used UISearchController rather than UISearchDisplayController.

If you want to activate the search, but don't want a search bar, all you have to do it activate the search using

self.searchController.active = YES;

and then because there is no search bar you have to implement the presentSearchController: methods of UISearchControllerDelegate protocol yourself.

- (void)presentSearchController:(UISearchController *)searchController {
   [self.window.rootViewController presentViewController:self.searchController animated:YES completion:nil];
}

It doesn't quite have the same animation, but I'm sure using the usual view controller animation api I can get something similar working.

lateAtNight
  • 482
  • 4
  • 13
  • Thanks for the helpful answer. Do you also experience the problem that the keyboard doesn't display automatically? I tried to call `self.searchController.searchBar becomeFirstResponder` in various places but it only works after the presenting animation is completed. So the searchBar is slided in first, then the keyboard is slided in. But I want them to animate in one go. For whatever reason, `searchBar.canBecomeFirstResponder` is always `NO` before the presenting animation is completed. Any idea? – Ortwin Gentz May 18 '15 at 12:03
  • To make it work the same as in Calendar app, I presented the search controller from the same view controller that owns it (i.e. `self` instead of `self.window.rootViewController`), and also set `searchController.hidesNavigationBarDuringPresentation = false` and `searchController.searchBar.searchBarStyle = .prominent`. – maxkonovalov Jun 07 '17 at 14:52