Friday, 20 July 2018

How to prevent the UISearchController from showing the navigation bar?

My use case is kind of strange. I'm using my own navigation bar, so I hide the default one with

[self.navigationController setNavigationBarHidden:YES animated:NO];

on viewWillAppear.

Everything works great, but if I have the keyboard open on an active search, and then I go back to a previous UIViewController, then the native navigation bar shows up again, and I end up with 2 navigation bars (my own and the default).

This is how I'm setting up my UISearchController in viewDidLoad:

- (void)viewDidLoad {
[super viewDidLoad];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.obscuresBackgroundDuringPresentation = NO;
self.searchController.searchBar.delegate = self;
[self.searchController.searchBar sizeToFit];
self.definesPresentationContext = YES;
self.searchController.hidesNavigationBarDuringPresentation = YES;
self.searchController.searchBar.searchBarStyle = UISearchBarStyleMinimal;
}

I already tried removing the self.definesPresentationContext = YES; (or turning it to NO), but that creates a different issue, which is that when I go back to the previous UIViewController the search bar stays on top of everything else! Until I tap on Cancel. I also tried calling the Cancel button programatically on viewWillDissappear, but that didn't work either...

So I'm running out of options, and that's why I'm here.

Any thoughts?



from How to prevent the UISearchController from showing the navigation bar?

No comments:

Post a Comment