I would like to replace the navigation bar with the search bar once shown and back.
OR
I would be fine if the navigation bar always laps over the content of my main view controller.
I set up the search controller like that:
_searchController = [[UISearchController alloc] initWithSearchResultsController:[[MySearchViewController alloc] init]];
_searchController.delegate = self;
_searchController.obscuresBackgroundDuringPresentation = TRUE;
_searchController.hidesNavigationBarDuringPresentation = TRUE;
_searchController.searchBar.placeholder = @"Search...";
_searchController.searchBar.delegate = self;
I show the search controller like that:
- (void)searchButtonTapped:(id)sender
{
self.navigationItem.searchController = _searchController;
[self.navigationController.view setNeedsLayout];
[self.navigationController.view layoutIfNeeded];
[self.navigationItem.searchController.searchBar becomeFirstResponder];
}
I hide the search controller like that:
- (void)willDismissSearchController:(UISearchController*)searchController
{
self.navigationItem.searchController = nil;
[self.navigationController.view setNeedsLayout];
}
I know there is the property searchController.hidesNavigationBarDuringPresentation which is close to what I want but exactly. Because the navigation bar only hides once I enter the search bar but I want it to hide already when I present the search bar.
As soon as I press the search icon button in the navigation bar it should be replaced with the search controller and only the search bar is being visible at the top. Does anyone know how to achieve this?
I already tried:
- to hide the navigation bar when I show the search controller with
[self.navigationController setNavigationBarHidden:YES animated:NO]; - to control the search bar height constraint and change it according visibilty
- to set the search controller to the
navigationItem.titleView-> it's not what I want - to hide the search controller according visivility -> the navigation bar remains with the same height only the search controller is hidden
- to use
self.definesPresentationContextbut this seems a different setting to what I need - had a look into
self.navigationItem.hidesSearchBarWhenScrollingbut this does not apply since I don't use a table view controller