问题描述
我使用此代码来更改我将其放在 AppDelegate 中的导航栏外观:
UINavigationBar.appearance().barTintColor = UIColor(hex: "E1354A")
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
我还使用以编程方式添加的 UISearchController。但是当我按下它时,导航栏和搜索控制器的颜色变为黑色。我不明白为什么会发生这种情况以及如何预防?
[
][1解决方法
我找到了解决办法。我放入 viewDidLoad:
let appearance = UINavigationBarAppearance()
appearance.backgroundColor = UIColor(hex: "E1354A")
appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
navigationItem.standardAppearance = appearance
navigationItem.scrollEdgeAppearance = appearance
let searchField = searchController.searchBar.searchTextField
searchField.backgroundColor = .systemBackground