iosuinavigationcontrolleruisearchbaruisearchcontroller

UISearchBar always appears with gray tint when placed in navigation bar via a search bar controller


I have been search for over two days now and simply could't find an answer. Hope anyone out there can help?

  1. In short, the project I am working on holds a searchbar in its navigationbar.
  2. This searchbar is part part of a UISearchController that gets passed to the searchController property of the view controller's navigationItem (see code example).
  3. Whatever I do, the search bar always appears with a gray tint.
  4. I set all possible background and tint colours to .clear or nil with no luck.
  5. Part of the challenge is that the searchbar has to stay in the navigation bar and cannot get placed anywhere else. All other space in the real navigation bar of the real project is already taken.
  6. I am suing Swift5 with minimum deployment iOS 14.

This sample code illustrates what we are using in our project:

class SearchControllerTests3: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .white
        navigationItem.title = "Search Bar"

        navigationController?.navigationBar.barTintColor = .white
        navigationController?.navigationBar.backgroundColor = .white
        navigationController?.navigationBar.tintColor = .white
        navigationController?.navigationBar.layer.borderWidth = 0.2

        let searchController = UISearchController()
        searchController.searchBar.backgroundColor = .white
        searchController.searchBar.tintColor = .white
        searchController.searchBar.searchTextField.backgroundColor = .white
        searchController.searchBar.searchTextField.tintColor = .white

        navigationItem.searchController = searchController

    }
}

The sample code above creates the following output when presented under a UINavigationController

enter image description here


Solution

  • After having searched for a long time, I did not find a way to fully customise a search bar that appears in the navigation bar as part of the a search bar controller.

    I resolved this issue by removing the search bar controller and adding a normal UISearchBar underneath the navigation bar. This set up gave me all the options I needed to gave the search bar the appearance I needed.