Search bar with behavior in the Maps application (full width of the navigation bar)

I am trying to display a search bar that will have the same behavior as in the native maps application. I mean:

  • search bar as the title bar of my navigation bar
  • When selected, the search bar takes up the full width of my navigation bar and I show the SearchDisplayController

So far I have managed to get the following behavior:

enter image description here

As you can see above, I cannot force the search bar to occupy full width when selected. Although the full width + cancel button seems to be the default for a search bar that is connected to the SearchDisplayController, at least if the search bar has not been added to the navigation bar!

Did I miss the obvious way to do this? Or do I need to customize the navigation bar myself when searchBarShouldBeginEditing is searchBarShouldBeginEditing ?

+7
ios iphone uinavigationbar uisearchbar uisearchdisplaycontroller
source share
2 answers

Until I have the perfect solution, I do the following. But I'm still open to something better.

 - (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar { self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancel)]; [UIView animateWithDuration:0.1 animations:^(){ self.navigationItem.leftBarButtonItem = nil; }]; return YES; } - (void)onCancel { [self.searchController setActive:NO]; } - (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar { [UIView animateWithDuration:0.1 animations:^(){ self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"OpenMenuButton.png"] style:UIBarButtonItemStylePlain target:self action:@selector(openSideMenu:)]; }]; self.navigationItem.rightBarButtonItem =self.doneButton; return YES; } 

In addition, I am wondering if the Maps application really uses a navigation controller, because it changes user behavior (width of titleView, animation of leftBarButton on the screen).

0
source share

Are you looking for something like UISearchDisplayController + UISearchBar ? http://petersteinberger.com/blog/2013/fixing-uisearchdisplaycontroller-on-ios-7/

0
source share

All Articles