iosswiftuinavigationbaruisearchcontroller

Navigation bar issue when search is active and push to next view controller


I am facing issue with navigation bar. I'm adding searchController in navigationItem's search controller.

See the images on following link: navigation bar issue

Steps:

1) I have data in table view, when I click on cell it's open details screen with custom navigation view. This is working fine. (default navigation bar is hidden)

2) Now, I have clicked on search bar and then click on table view cell. It's show me default navigation bar to details screen. I don't want to display default navigation bar.

Code that I did write to implement search controller is as follows:

searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."

searchController.searchBar.tintColor = .white
searchController.searchBar.barTintColor = .white

if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField {

    textfield.textColor = UIColor.blue

    if let backgroundview = textfield.subviews.first {

        // Background color
        backgroundview.backgroundColor = UIColor.white

        // Rounded corner
        backgroundview.layer.cornerRadius = 10;
        backgroundview.clipsToBounds = true;
    }
}

self.navigationItem.searchController = self.searchController

definesPresentationContext = true

Below is code to hide navigation bar inside didSelect method:

self.navigationController?.navigationBar.isHidden = true
self.navigationController?.isNavigationBarHidden = true

Solution

  • You can fix this issue by making search controller inactive, then navigate to your details view controller after some delay.

    Try following code in you didSelect method which will help you to hide navigation bar when search controller is active.

    searchController.isActive = false
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
        self.navigationController?.navigationBar.isHidden = true
        self.navigationController?.isNavigationBarHidden = true
        self.navigationController?.pushViewController(<YourViewController>, animated: true)
    }
    

    You must required delay to navigate otherwise it's give you warning in console regarding navigation controller presentation process.

    So, this code first make your search controller inactive and then navigate to your next view controller.