iOS 11在导航栏中自定义search栏

我想在iOS 11search栏中embedded导航栏时更改文本和图标的颜色。 所以占位符文本,search文本和search图标。

在这里输入图像说明

if #available(iOS 11.0, *) { navigationController?.navigationBar.prefersLargeTitles = false let searchController = UISearchController(searchResultsController: nil) navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false searchController.searchBar.placeholder = "Suchen" searchController.searchBar.tintColor = .white } 

正如您在图片中看到的,深蓝色背景上的文字是灰色的,看起来很丑。 我想文字和图标至less是白色的。 (改变蓝色背景颜色也不行,真的很好,看我的其他问题 )

唯一可行的是改变闪烁的光标的颜色和“取消”button,这是用.tintColor属性完成的。

似乎在iOS 10及以下版本中工作的解决scheme似乎不适用于iOS 11,所以请仅发布您知道在iOS 11中工作的解决scheme。谢谢。

也许我错过了关于这个“自动造型”在iOS 11的观点。任何帮助表示赞赏。

我刚刚发现如何设置其余的(在布兰登的帮助下,谢谢!)

“取消”文本:

 searchController.searchBar.tintColor = .white 

search图标:

 searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal) 

清晰的图标:

 searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal) 

search文本:

 UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white] 

感谢@Brandon的帮助!

在这里输入图像说明

占位符:

 UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white]) 

在这里输入图像说明

白色背景:

 if #available(iOS 11.0, *) { let sc = UISearchController(searchResultsController: nil) sc.delegate = self let scb = sc.searchBar scb.tintColor = UIColor.white scb.barTintColor = UIColor.white if let textfield = scb.value(forKey: "searchField") as? UITextField { textfield.textColor = UIColor.blue if let backgroundview = textfield.subviews.first { // Background color backgroundview.backgroundColor = UIColor.white // Rounded corner backgroundview.layer.cornerRadius = 10; backgroundview.clipsToBounds = true; } } if let navigationbar = self.navigationController?.navigationBar { navigationbar.barTintColor = UIColor.blue } navigationItem.searchController = sc navigationItem.hidesSearchBarWhenScrolling = false } 

在这里输入图像说明

从这里采取。

设置search文本颜色

 (UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 

设置search占位符颜色

 (UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]) ).attributedPlaceholder = [NSForegroundColorAttributeName: UIColor.white] 

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

AppDelegate UISearchBar.appearance().tintColor = UIColor.white

或者,把它们都在[UIViewController viewDidLoad:]

此代码更改文本字段的背景颜色。

斯威夫特4

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { //background color of text field UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = .cyan }