derdida
derdida

Reputation: 14904

Navigation Bar Search Results

I would like to add an search directly to my navigation bar (like Apple Maps).

enter image description here

So thats no the problem, I'll just create it as Searchbar:

    searchBar.placeholder = "Gebäude und Räume suchen"
    var rightNavBarButton = UIBarButtonItem(customView: searchBar)
    self.navigationItem.rightBarButtonItem = rightNavBarButton

But how should i solve to see the results? First ill tried to use an Adaptive Popover, and if the user enters at least 2 letters ill load the popover and show the results (so I am able to show an Popover on the iPad, and get a Fullscreen View on the iPhone) - but here i need to check on every keypress if the popover is still visible, AND if there is an adaptive popover I need to create the searchbar on the child-controller too.

So I am not sure how to solve such things like Apple Maps (iPhone and iPad Version) - can anyone give me some tipps how can I do that? Is that a Popover? A Custom View?

Edit:

The easiest way (and this is how its atm looks like) - I have a Button with an Search Icon, and if the user clicks on this SearchButton, ill load an "Search" Controller (with an Searchbar) - if the user clicks on one Result ill go "back" and show the result on the Map.

But I would prefer to make it like the Apple Maps App (with the difference between iPad and iPhone Version)

Upvotes: 0

Views: 178

Answers (2)

EmilDo
EmilDo

Reputation: 1177

What apple do in iPad is using a UIPopover, i have previously done exactly the same thing for custom Maps app using the following delegates:

-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    //init a searchPopover with the view you want to show the results in and then call it 
    [self.searchPopover presentPopoverFromRect:[self.search bounds] inView:self.search permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)aSearchBar
{
    [self.searchPopover dismissPopoverAnimated:YES];;
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    //update the tableview that is in the searchPopover
    //searchView is the view inside the popOver, filterResults is a custom method with an NSPredicate inside. 
    [self.searchView filterResultsUsingString:searchText];
}

In the view controller that will display the result, you implement delegation, so that when the user presses the desired button or row in that view, the parentview is notidifed and the popover is dismissed, you zoom on the annotation you want.

-(void)didSelectResult:(id)annotation
{   
    //so when you select the result, and call this delegate method:
    self.annotationToSelect = annotation;
    [self.searchBar resignFirstResponder];
    self.search.text = @"";
    [self.searchPopover dismissPopoverAnimated:YES];

}

Upvotes: 1

Mundi
Mundi

Reputation: 80271

I can see why this is confusing. The functionality you are looking for used to be provided by UISearchDisplayController and UISearchDisplayDelegate. But these methods are deprecated in iOS 8.

The way to go these days is to use a UISearchController. The docs explain it pretty well:

You create a new search controller using the initWithSearchResultsController: method, passing in the view controller that manages the contents to be displayed. The searchResultsUpdater property contains the object that is responsible for updating the results. Often the object contained in the searchResultsUpdater property is the same as the view controller set during initialization. However, if you have created your own model object to filter and respond to queries, you can set it as the searchResultsUpdater.

The callback mechanism for presenting and dismissing the search results is provided by the UISearchControllerDelegate.

Upvotes: 1

Related Questions