I have a navigation bar which is displayed without a search bar on it when the screen first displayed. I trigger and display the search bar in Navigation bar but I am wondering how to hide the search bar back and display Navigation bar title again.
I create the search bar as following:
_searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.searchBar.placeholder = nil;
[self.searchController.searchBar sizeToFit];
//self.tableView.tableHeaderView = self.searchController.searchBar;
self.sharedNavigationItem.titleView = _searchController.searchBar;
self.searchController.delegate = self;
self.searchController.dimsBackgroundDuringPresentation = NO; // default is YES
self.searchController.searchBar.delegate = self; // so we can monitor text changes + others
self.definesPresentationContext = YES;
_searchController.hidesNavigationBarDuringPresentation = NO;
I want to hide search bar only here and display a normal navigation bar with a title:
I had the same issue. Previous answers suggest to scrap the whole NavigationBar
, but it is not the researched behaviour. The solution is to add the searchBar
when the search button is pressed...
@IBAction func didPressAddButton(_ sender: Any) {
if #available(iOS 11.0, *) {
// For iOS 11 and later, place the search bar in the navigation bar.
navigationItem.searchController = tagSearchController
navigationItem.hidesSearchBarWhenScrolling = true
} else {
// For iOS 10 and earlier, place the search controller's search bar in the table view's header.
tableView.tableHeaderView = tagSearchController.searchBar
}
tagSearchController.isActive = true
}
...and remove it when didDismissSearchController
is called (UISearchControllerDelegate
method).
func didDismissSearchController(_ searchController: UISearchController) {
if #available(iOS 11.0, *) {
// For iOS 11 and later, place the search bar in the navigation bar.
navigationItem.searchController = nil
} else {
// For iOS 10 and earlier, place the search controller's search bar in the table view's header.
tableView.tableHeaderView = nil
}
}
NB: my searchController
is created and configured in viewDidLoad()
If you want to hide entire navigation bar and if you want to use your own navigation bar, you can first hide navigation controller's navigation bar.
[self.navigationController setNavigationBarHidden:YES];
//OR
[self.navigationController setNavigationBarHidden:YES animated:NO];
or if you have used custom title view for navigation bar, you can do
self.navigationItem.titleView.hidden = YES;
or if you want to hide back bar button item, you can do
self.navigationItem.hidesBackButton = TRUE;
or if you want to only hide title you can do
self.navigationItem.title = @"";
And
self.navigationController.navigationItem.titleView = nil ;
You can try adding below the property.
hidesNavigationBarDuringPresentation.
For ref:
https://stackoverflow.com/a/12529945/3799720
Another ref:
- (void)viewDidLayoutSubviews {
[self.navigationController setNavigationBarHidden:NO animated:NO];
}
UISearchDisplayController hiding navigation bar
Note: Not tested
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With