我知道如何设置独立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
}
但这不适用于UISearchController
。
我想将占位符和左放大镜图标的文本颜色设置为纯白色。 (现在看来它们上面有一层彩色的层。)
另外,输入文字现在是黑色的,我也希望它是白色的。
总之,我想修改以下属性
1. textField背景颜色
2. textFiled占位符文字颜色
3. textFiled文字颜色
4. textFiled font
任何人都知道怎么做?谢谢你提前。
在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公司:
将这些设置放在ViewDidAppear()
确实解决了我的问题的一部分
但是当我设置条形背景颜色时textfield's background color
发生了变化
由于searchBar.barTintColor = .red
无法在导航项中嵌入的iOS11' UISearchController
中使用,因此我使用了searchBar.backgroundColor = .red
这让我很困惑
那么如何分别改变searchBar的背景和textField的背景呢?
答案 0 :(得分:3)
为搜索栏的文本字段设置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
}
}
结果如下:
https://github.com/Electron/Windows-Installer
只需在此代码中应用颜色组合即可。
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
}
<强>结果:强>
答案 1 :(得分:2)
在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
更新 - 以下是自定义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
}
}
}
答案 2 :(得分:1)
已接受的解决方案不适用于iOS 13,您遇到以下错误(带有Obj-C代码的睾丸):
由于未捕获的异常“ NSGenericException”而终止应用程序, 原因:“禁止访问UISearchBar的_searchField ivar。这个 是一个应用程序错误'
但是现在您可以选择直接访问UISearchBar的TextField,而无需使用私有API。
{=INDEX(Move!G:G,(MATCH(1,($B36/2.8=Move!$C:$C)*($A36=Move!$A:$A)*($C36=Move!$D:$D)*($D36=Move!$E:$E)*($E36=Move!$B:$B),0)))}