问题
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