I'm in the process of refactoring UISearchDisplayController with UISearchController and I've run into a snag.
I'm using a separate ResultsViewController with the SearchController in order to allow the "dimmed" view when search is active but no text has been entered and the "active" results view that shows results once a user has started typing.
I also have UISearchController.hidesNavigationBarWhenPresenting set to YES (default)
First fun fact was finding how to push a view controller from the results view controller on didSelectRowAtIndexPath:(NSIndexPath *)indexPath.
I got that to work by calling [self.presentingViewController.navigationController pushViewController: myViewController animated:YES];
But, when I go to the pushed view controller, the navigation bar appears on the pushed controller. Setting [self.navigationController setNavigationBarHidden:YES animated:animated]; doesn't help.
The problem appears to be when the search controller hides the navigation bar when it becomes active. I can't find a way to smoothly transition the searchController out of active state before pushing to the view controller in didSelectRowAtIndexPath
One way that works is to set hidesNavigationBarWhenPresenting to NO. This appraoch keeps UISearchController from messing with the navigation bar at all, so my pushed VC can hide the navigation bar when it appears.
The other way I found that works is this (and it *****).
This is all inside my main TableViewController with the SearchController.
Inside didselectRowAtIndexPath, I check if the searchController is active, and if it is, I set it to not be active, which dismisses it.
Then I have to implement UISearchControllerDelegate and implement didDismissSearchController and push my view controller inside this method.
I also have to add logic to viewWillAppear that enables the serach if the controller finds a search string.
The result is not a smooth transition. When you click on an item in the search results, you'll see the search dismiss itself before the app pushes the new view controller. If you come back to the main TableViewController with search, you'll see the table view controller load with all results before the search controller becomes active.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:NO animated:YES];
if ([self.searchString length] > 0) {
[self.searchController setActive:YES];
self.searchController.searchBar.text = self.searchString;
[self.searchController.searchBar becomeFirstResponder];
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
....
if ([self.searchController isActive]) {
[self.searchController setActive:NO];
} else {
[self.navigationController pushViewController:self.loginViewController animated:YES];
}
}
#pragma mark UISearchControllerDelegate methods
- (void)didDismissSearchController:(UISearchController *)searchController {
if (self.loginViewController != nil) {
if (self.navigationController) {
[self.navigationController pushViewController:self.loginViewController animated:YES];
}
}
}I also tried moving [self.navigationController setNavigationBarHidden:YES animated:animated]; inside the viewDidAppear method of the pushed view contorller, and what I see is the navigation bar slide up to hide itself, then immediately slide back down.
What I want is a way to do this:
1) User selects a row in the results view controller
2) Results view controller pushes a new view controller
3) As the transition happens - the main VC with searchController should become inactive and allow the navigation bar to return.
4) When the pushed VC appears, it should be able to use setNavigationBarHidden to hide the nav bar.
Is there any way to do this smoothly?