Search code examples
swiftuinavigationcontrolleruitabbarcontrolleruisearchbar

White lines appear in SearchBar when using colored navigation bar


I am getting multiple white lines when clicking in the SearchBar.

enter image description here

It is happening when using both a TabBarController and a colored bar NavigationController, but

  • It works when only using a NavigationController
  • It works when both TabBarController and NavigationController but with the default color

enter image description here


I setup the navigation color in the AppDelegate using this line of code:

UINavigationBar.appearance().barTintColor =  UIColor(rgb: 0x0277BD)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]

And I setup the UISearchController in my SearchViewController using:

let searchController = UISearchController(searchResultsController: nil)
    override func viewDidLoad() {
        super.viewDidLoad()
        // Setup the Search Controller

        searchController.searchResultsUpdater = self
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = "Search Events"
        searchController.searchBar.tintColor = .white
        navigationItem.searchController = searchController
        definesPresentationContext = true
}

Any idea of what is happening?


Solution

  • Not sure if this is a satisfying answer but it looks like an iOS bug, that might have something to do with the translucency effect that's added by default to the top bar. The top bar consists of two parts (navigation & search) and it seems like the white line appears on the bottom edge of the navigation part during the slide-up animation. If you add navigationController?.navigationBar.isTranslucent = false to your viewDidLoad() the problem goes away.

    Translucent bar Translucent bar

    Opaque bar Opaque bar

    Why does the white line appear only when you embed the UINavigationController in the UITabBarController? No idea :( The isTranslucent = false thing is a workaround at best, but maybe it's enough.