作者:聆听最遥远的歌声 | 来源:互联网 | 2023-01-23 12:45
我之前使用iOS 11之前的代码来自定义UISearchController
搜索栏的外观:
var searchCOntroller= UISearchController(searchResultsController: nil)
searchController.searchBar.setDefaultSearchBar()
searchController.searchResultsUpdater = self
if #available(iOS 11.0, *) {
navigationItem.searchCOntroller= searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
extension UISearchBar {
func setDefaultSearchBar() {
self.tintColor = UIColor.blue
self.searchBarStyle = .minimal
self.backgroundImage = UIImage(color: UIColor.clear)
let searchBarTextField = self.value(forKey: "searchField") as! UITextField
searchBarTextField.textColor = UIColor.white
searchBarTextField.tintColor = UIColor.blue
searchBarTextField = .dark
}
}
但是,更新到iOS 11并添加任何自定义的UISearchController
一部分(除了键盘外观证明我的代码已被调用)无法显示.这是一个错误还是我错误地实现了它? navigationItem
编辑:
附件是运行iOS 10(第一个)和iOS 11的设备的两个屏幕截图
编辑2:到目前为止,关于这个问题的大部分注意力都集中在文本的颜色上UISearchBar
.我在看比这更多-的背景颜色,色调的颜色,搜索指示器和清除按钮颜色等所有不能尽管是完全能够在iOS的10这么做运行iOS 11的设备上进行定制.
1> Darko..:
我刚刚发现了如何设置它们(在Brandon和Krunal的帮助下,谢谢!)
"取消"文字:
searchController.searchBar.tintColor = .white
搜索图标:
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)
搜索文字:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
占位符:
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
}
取自这里.
为什么每次释放苹果都会有所作为
这个答案再次对颜色不太好.它们的导航栏背景颜色总是比您指定的颜色更暗.
2> Zyntx..:
要将键入搜索栏的文本正确设置为白色(使用暗场颜色时):
searchController.searchBar.barStyle = .black
设置文本字段背景颜色
if #available(iOS 11.0, *) {
if let textfield = searchController.searchBar.value(forKey: "searchField") as? UITextField {
if let backgroundview = textfield.subviews.first {
// Background color
backgroundview.backgroundColor = UIColor.white
// Rounded corner
backgroundview.layer.cornerRadius = 10;
backgroundview.clipsToBounds = true;
}
}
}
但是使用类似的东西
textfield.textColor = UIColor.blue
在上面似乎没有用.
3> 小智..:
尝试设置搜索栏的栏样式。
searchController.searchBar.barStyle = UIBarStyleBlack;