UISearchController iOS 11 自定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45663169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UISearchController iOS 11 Customization
提问by Alexander MacLeod
I had been using the following code prior to iOS 11 to customize the appearance of the UISearchController
search bar:
在 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
}
}
However, the appearance of the search bar fails to update when running the same code on iOS 11.
但是,在 iOS 11 上运行相同的代码时,搜索栏的外观无法更新。
iOS 10:
iOS 10:
iOS 11:
iOS 11:
Much of the attention to this question so far has focused on the text color of the search bar. I am looking at more than this - the background color, tint color, the search indicator, clear button color, etc.
到目前为止,对这个问题的大部分注意力都集中在搜索栏的文本颜色上。我看的远不止这些 - 背景颜色、色调颜色、搜索指示器、清除按钮颜色等。
回答by Darko
I just found out how to set them: (with some help of Brandon and Krunal, thanks!)
我刚刚找到了如何设置它们:(在 Brandon 和 Krunal 的帮助下,谢谢!)
The "Cancel" text:
“取消”文本:
searchController.searchBar.tintColor = .white
The search icon:
搜索图标:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.search, state: .normal)
The clear icon:
清晰的图标:
searchController.searchBar.setImage(UIImage(named: "my_search_icon"), for: UISearchBarIcon.clear, state: .normal)
The search text:
搜索文本:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
The placeholder:
占位符:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
The white background:
白色背景:
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
}
Taken from here.
取自这里。
回答by Zyntx
To properly set the text typed into the search bar to white use (when using a dark field color):
要将输入到搜索栏中的文本正确设置为白色(使用暗字段颜色时):
searchController.searchBar.barStyle = .black
To set the textfield background color
设置文本框背景颜色
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;
}
}
}
However using something like
但是使用类似的东西
textfield.textColor = UIColor.blue
in the above does not seem to work.
在上面似乎不起作用。
回答by Eddie Hsu
回答by Ben Sinclair
Moving the call to setDefaultSearchBar
into viewDidAppear
should fix this.
将调用移动到setDefaultSearchBar
intoviewDidAppear
应该可以解决此问题。
回答by Jonny
You need to find the UISearchBar
's underlying UITextField
and change its text color.
您需要找到UISearchBar
的底层UITextField
并更改其文本颜色。
Notice this only have effect when search controller is going to present (UISearchControllerDelegate.willPresentSearchController
) or presented.
请注意,这仅在搜索控制器将要呈现 ( UISearchControllerDelegate.willPresentSearchController
) 或呈现时有效。
class ViewController : UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// setup your search controller...
// set search controller's delegate
navigationItem.searchController?.delegate = self
}
}
extension ViewController : UISearchControllerDelegate {
func willPresentSearchController(_ searchController: UISearchController) {
// update text color
searchController.searchBar.textField?.textColor = .white
}
}
extension UISearchBar {
var textField: UITextField? {
for subview in subviews.first?.subviews ?? [] {
if let textField = subview as? UITextField {
return textField
}
}
return nil
}
}
回答by Isa M
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
回答by Hoff Silva
Try it: searchController.<YOUR SEARCHBAR>.barStyle = .blackOpaque
instead of self.searchBarStyle = .minimal
.
试试看:searchController.<YOUR SEARCHBAR>.barStyle = .blackOpaque
而不是self.searchBarStyle = .minimal
.
Thus:
因此:
var searchController = UISearchController(searchResultsController: nil)
searchController.searchBar.setDefaultSearchBar()
//Add this line below
searchController.searchBar.barStyle = .blackOpaque
searchController.searchResultsUpdater = self
if #available(iOS 11.0, *) {
navigationItem.searchController = searchController
} else {
tableView.tableHeaderView = searchController.searchBar
}
extension UISearchBar {
func setDefaultSearchBar() {
self.tintColor = UIColor.blue
//Delete this line below
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
}
}
回答by Adam Studenic
If you need to change the background colour of the textField in the searchBar, see my answer here: https://stackoverflow.com/a/46315974/1109892
如果您需要更改搜索栏中 textField 的背景颜色,请在此处查看我的答案:https://stackoverflow.com/a/46315974/1109892