Adding SearchController to navigationItem causes status bar to change to black when active

邮差的信 提交于 2021-01-28 09:22:32

问题


This particular view is part of a UINavigationController, so the navigation bar is provided by that. I call the following code in viewDidLoad() of the presenting view controller.

func buildSearchBar() {
    self.definesPresentationContext = true
    let searchController = UISearchController(searchResultsController: nil)
    self.navigationItem.searchController = searchController
    self.navigationItem.hidesSearchBarWhenScrolling = false
    self.navigationController?.navigationBar.barTintColor = UIColor(displayP3Red: 30/255, green: 50/255, blue: 49/255, alpha: 1.0)
    searchController.searchBar.backgroundColor = UIColor(displayP3Red: 30/255, green: 50/255, blue: 49/255, alpha: 1.0)
}

Using interface builder, I set the same color for the navigation bar for the navigation controller.

When the search bar is active, the background of the status bar goes to black.

This is inactive.

This is active.

I have seen this problem on StackOverflow many times, and there has not been a solution that works. Seems like, for most people, it works out of the box once they set up the Search Controller. Most recent examples use similar code and don't mention having this problem.


回答1:


I used both iPhone and simulators in Xcode 10.1 and osx 10.14.1 and did not repeat the issue.

If you want to get the same color, just add self.navigationController?.navigationBar.isTranslucent = false

   func buildSearchBar() {
    self.definesPresentationContext = true
    let searchController = UISearchController(searchResultsController: nil)
    self.navigationItem.searchController = searchController
    self.navigationItem.hidesSearchBarWhenScrolling = false
    self.navigationController?.navigationBar.barTintColor = UIColor(displayP3Red: 30/255, green: 50/255, blue: 49/255, alpha: 1.0)
    self.navigationController?.navigationBar.isTranslucent = false
    searchController.searchBar.backgroundColor = UIColor(displayP3Red: 30/255, green: 50/255, blue: 49/255, alpha: 1.0)
}



来源:https://stackoverflow.com/questions/53184389/adding-searchcontroller-to-navigationitem-causes-status-bar-to-change-to-black-w

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!