[英]How to customize the searchBar in a UISearchController?
I know how to set the appearance for a independent UISearchBar
, just like the following. 我知道如何为独立的
UISearchBar
设置外观,如下所示。
let searchField = searchBar.value(forKey: "searchField") as? UITextField
if let field = searchField {
field.backgroundColor = UIColor.defaultBackgroundColor
field.layer.cornerRadius = 15.0
field.textColor = .white
field.tintColor = .white
field.font = UIFont.systemFont(ofSize: fl(13))
field.layer.masksToBounds = true
field.returnKeyType = .search
}
But this is not working in the UISearchController
. 但这不适用于
UISearchController
。
I want to set the text color of the placeholder and the left magnifying lens icon to pure white. 我想将占位符的文本颜色和左放大镜图标设置为纯白色。 (It seems there is a colored layer over them now).
(现在看来它们上面有彩色层)。
In addition, the input text is black now, I want it to be white too. 另外,输入文字现在是黑色的,我也希望它是白色的。
In a conclusion, I want to modify the following properties. 总之,我想修改以下属性。
1. textField background color 1. textField背景颜色
2. textFiled placeholder text color 2. textFiled占位符文本颜色
3. textFiled text color 3. textFiled文字颜色
4. textFiled font 4. textFiled字体
Anyone know how do it? 谁知道怎么做?
Add the following with your code in viewDidAppear: 在viewDidAppear中添加以下代码:
let placeholderString = NSAttributedString(string: "Placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
field.attributedPlaceholder = placeholderString
let iconView = field.leftView as! UIImageView
iconView.image = iconView.image?.withRenderingMode(.alwaysTemplate)
iconView.tintColor = .white
Updata: Updata公司:
Put those settings in ViewDidAppear()
did solve a part of my problem. 把这些设置放在
ViewDidAppear()
确实解决了我的一部分问题。
But the textfield's background color
changed when I set the bar's background color. 但是当我设置条形图的背景颜色时,
textfield's background color
发生了变化。
Because searchBar.barTintColor = .red
is not working in iOS11's UISearchController
embedded in navigation item, I used searchBar.backgroundColor = .red
因为
searchBar.barTintColor = .red
.red在导航项中嵌入的iOS11的UISearchController
中不起作用,所以我使用了searchBar.backgroundColor = .red
It confused me a lot. 这让我很困惑。
So how to change searchBar's background and textField's background separately? 那么如何分别改变searchBar的背景和textField的背景呢?
set attributedPlaceholder
for textfield of search bar 为搜索栏的文本字段设置
attributedPlaceholder
@IBOutlet weak var sbSearchBar: UISearchBar!
if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {
textfield.backgroundColor = UIColor.red
textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedStringKey.foregroundColor : UIColor.white])
if let leftView = textfield.leftView as? UIImageView {
leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)
leftView.tintColor = UIColor.white
}
}
Here is result: 结果如下:
I think, this may help you: how to change uitextfield color in searchcontroller? 我想,这可能会对你有所帮助: 如何在searchcontroller中更改uitextfield颜色?
Just apply your color combination in this code and see. 只需在此代码中应用颜色组合即可。
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 = // Set text color
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
}
Result: 结果:
Add the following with your code in viewDidAppear: 在viewDidAppear中添加以下代码:
let placeholderString = NSAttributedString(string: "Placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.white])
field.attributedPlaceholder = placeholderString
let iconView = field.leftView as! UIImageView
iconView.image = iconView.image?.withRenderingMode(.alwaysTemplate)
iconView.tintColor = .white
Update - the following is the complete code to customize UISearchController colors: 更新 - 以下是自定义UISearchController颜色的完整代码:
override func viewDidAppear(_ animated: Bool) {
//sets navigationbar backgroundColor
if let navigationbar = self.navigationController?.navigationBar {
navigationbar.barTintColor = UIColor.magenta
}
let searchField = searchController.searchBar.value(forKey: "searchField") as? UITextField
//sets searchBar backgroundColor
searchController.searchBar.backgroundColor = .blue
if let field = searchField {
field.layer.cornerRadius = 15.0
//sets text Color
field.textColor = .brown
//sets indicator and cancel button Color
field.tintColor = .green
field.font = UIFont.systemFont(ofSize: 13)
field.layer.masksToBounds = true
field.returnKeyType = .search
//sets placeholder text Color
let placeholderString = NSAttributedString(string: "placeholder", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
field.attributedPlaceholder = placeholderString
//sets icon Color
let iconView = field.leftView as! UIImageView
iconView.image = iconView.image?.withRenderingMode(.alwaysTemplate)
iconView.tintColor = .cyan
//sets textField backgroundColor
if let backgroundview = field.subviews.first {
backgroundview.backgroundColor = UIColor.yellow
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.