【发布时间】:2019-07-19 20:20:56
【问题描述】:
我觉得我正在服用疯狂的药丸。我想更改 UISearchBar 中文本字段的颜色,该 UISearchBar 托管在 UINavigationBar 中。我遇到的问题是 UISearchBar 获取 UINavigationBar 的颜色,但 TextField 没有改变颜色。我在这里尝试了几种解决方案,但似乎都没有解决问题。任何想法如何改变 TextField 的颜色?
我知道还有其他类似的问题,但我尝试了所有可能遇到的解决方案,但没有找到解决我问题的解决方案。
当前代码:
let sc = UISearchController(searchResultsController: nil)
sc.delegate = self
let scb = sc.searchBar
scb.tintColor = UIColor.green
scb.barTintColor = UIColor.yellow
if let textfield = scb.value(forKey: "searchField") as? UITextField {
textfield.textColor = UIColor.blue
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.red
// Rounded corner
backgroundview.layer.cornerRadius = 5;
backgroundview.clipsToBounds = true;
}
}
navigationController?.navigationBar.barTintColor = UIColor.blue
navigationItem.searchController = sc
navigationItem.hidesSearchBarWhenScrolling = false
我创建了一个sample project 来展示它的实际效果。任何帮助或指导将不胜感激!
【问题讨论】:
标签: ios uinavigationbar uisearchbar ios12 ios13