Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 834
Mayur Karmur Avatar asked Jul 18 '18 12:07

Mayur Karmur


1 Answers

I had the same problem. It seems that something make the navigation bar visible when search controller was active in previous view although I hide the navigation bar in viewWillAppear().

I solved it by hide the navigation bar again in viewWillLayoutSubviews():

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    navigationController?.setNavigationBarHidden(true, animated: false)
}
like image 125
Dave Avatar answered Nov 15 '22 22:11

Dave