Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigating to a view (push) from a search results table (UISearchController) and keeping the search results in place

I have a search results table (UISearchController) that is presented on a view that exists on a navigation stack. I present the search controller and table on my view like this:

    _searchResultsTableViewController = [[CalendarSearchResultsTableViewController alloc] init];
_searchResultsTableViewController.delegate = self;

_searchController = [[UISearchController alloc] initWithSearchResultsController:_searchResultsTableViewController];
_searchController.delegate = self;
_searchController.hidesNavigationBarDuringPresentation = NO;
_searchController.dimsBackgroundDuringPresentation = YES;
_searchController.obscuresBackgroundDuringPresentation = YES;
_searchController.active = YES;

_searchController.searchBar.delegate = self;

UIEdgeInsets adjustForTabbarInsets = UIEdgeInsetsMake(0, 0, CGRectGetHeight(self.tabBarController.tabBar.frame), 0);
_searchResultsTableViewController.tableView.contentInset = adjustForTabbarInsets;
_searchResultsTableViewController.tableView.scrollIndicatorInsets = adjustForTabbarInsets;

[self.navigationController presentViewController:_searchController animated:YES completion:nil];

This presents a UISearchBar over the UINavigationBar and the search table appears as expected. When the user selects a search result from the table, I want to present a view for the selection by pushing it onto the navigation stack while the search results remain in place. The way I have it working now is that the view that presented the search controller opens the view, but this requires the search controller to be dismissed because the search result view is presented behind the search results table.

How can I make it so that the search controller pushes the search result view onto the navigation stack, allowing the user to navigate back to the search results?

An example of how I expect this to work can be found in the iOS Mail of Calendar app (when you search).

My navigationController property in the search results controller is nil, so I cannot currently present the search result view from there.

The project is in both Objective-C and Swift, so answers in either is fine. Any help is much appreciated!

like image 706
timgcarlson Avatar asked Jan 19 '17 17:01

timgcarlson


1 Answers

I think there are 3 small changes you need to make.

  1. _searchController.hidesNavigationBarDuringPresentation = YES;
  2. [self presentViewController:_searchController animated:YES completion:nil];
  3. self.definesPresentationContext = YES; // in viewDidLoad of the viewController that presented the searchController
like image 177
Warif Akhand Rishi Avatar answered Sep 17 '22 13:09

Warif Akhand Rishi