[英]How to customize the searchBar in a UISearchController?
我知道如何为独立的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字体
谁知道怎么做?
在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
.red在导航项中嵌入的iOS11的UISearchController
中不起作用,所以我使用了searchBar.backgroundColor = .red
这让我很困惑。
那么如何分别改变searchBar的背景和textField的背景呢?
为搜索栏的文本字段设置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
}
}
结果如下:
我想,这可能会对你有所帮助: 如何在searchcontroller中更改uitextfield颜色?
只需在此代码中应用颜色组合即可。
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
}
结果:
在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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.