How to hide UISearchController in UINavigationBar

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()


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 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 ;