UISearchController iOS 11自定义 [英] UISearchController iOS 11 Customization
问题描述
我在iOS 11之前使用以下代码来自定义 UISearchController
搜索栏的外观:
I had been using the following code prior iOS 11 to customize the appearance of the UISearchController
search bar:
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
作为<$的一部分c $ c> navigationItem 任何自定义(除了证明我的代码已被调用的键盘外观)都无法显示。这是一个错误还是我错误地实现了它?
However, having updated to iOS 11 and adding the UISearchController
as part of the navigationItem
any customization (except the keyboard appearance proving my code has been called) fails to show. Is this a bug or have I implemented it incorrectly?
编辑:
附件是运行iOS 10(第一个)和iOS 11的设备的两个屏幕截图
Attached are two screenshots of devices running iOS 10 (first) and iOS 11
编辑2:到目前为止,关于这个问题的大部分注意力都集中在 UISearchBar
的文字颜色上。我正在考虑更多 - 背景颜色,色调颜色,搜索指示器和清除按钮颜色等。所有无法在运行iOS 11的设备上进行自定义,尽管完全能够在iOS 10上这样做。
EDIT 2: Much of the attention on this question so far focuses on the text color of the UISearchBar
. I am looking at more than this - the background color, tint color, the search indicator and clear button colors, etc. all are not able to be customized on a device running iOS 11 despite being perfectly able to do so at iOS 10.
推荐答案
我刚刚找到了如何设置它们:(在Brandon和Krunal的帮助下,谢谢!)
I just found out how to set them: (with some help of Brandon and Krunal, thanks!)
取消文字:
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
}
取自这里。
这篇关于UISearchController iOS 11自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!