searchIcon
颜色。
搜索图标位于搜索文本框的左视图中。由于这是可访问的,因此我们可以轻松设置视图的tintColor。 tintColor也用于搜索图标。
因此,在您的代码中,您可以使用以下代码将搜索图标更改为白色:
searchBar.searchTextField.leftView?.tintColor = .white
我找到的最佳解决方案在这里:Changing the color of the icons in a UItextField inside a UISearchBar
(此解决方案使用原始的UISearchBar图标,无需提供自己的图形资源)
转换为Swift(Swift 3):
if let textFieldInsideSearchBar = self.searchBar.value(forKey: "searchField") as? UITextField,
let glassIconView = textFieldInsideSearchBar.leftView as? UIImageView {
//Magnifying glass
glassIconView.image = glassIconView.image?.withRenderingMode(.alwaysTemplate)
glassIconView.tintColor = .whiteColor
}
如果您使用白色搜索图标的自定义图片,搜索栏将不会修改您提供的图片。要设置自定义图像,请使用此方法。(文档链接。)
- (void)setImage:(UIImage *)iconImage
forSearchBarIcon:(UISearchBarIcon)icon
state:(UIControlState)state;
示例代码:
[searchBar setImage:[UIImage imageNamed:@"SearchIcon"]
forSearchBarIcon:UISearchBarIconSearch
state:UIControlStateNormal];
在Swift中:
searchBar.setImage(UIImage(named: "SearchIcon"), for: .search, state: .normal)
这个解决方案对我在Swift 3.0中的Xcode 8.2.1有效:
extension UISearchBar
{
func setPlaceholderTextColorTo(color: UIColor)
{
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = color
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = color
}
func setMagnifyingGlassColorTo(color: UIColor)
{
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
let glassIconView = textFieldInsideSearchBar?.leftView as? UIImageView
glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
glassIconView?.tintColor = color
}
}
使用示例:
searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)
searchController.searchBar.setMagnifyingGlassColorTo(color: mainColor)
Swift-3:
extension UISearchBar
{
func setMagnifyingGlassColorTo(color: UIColor)
{
// Search Icon
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
let glassIconView = textFieldInsideSearchBar?.leftView as? UIImageView
glassIconView?.image = glassIconView?.image?.withRenderingMode(.alwaysTemplate)
glassIconView?.tintColor = color
}
func setClearButtonColorTo(color: UIColor)
{
// Clear Button
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
let crossIconView = textFieldInsideSearchBar?.value(forKey: "clearButton") as? UIButton
crossIconView?.setImage(crossIconView?.currentImage?.withRenderingMode(.alwaysTemplate), for: .normal)
crossIconView?.tintColor = color
}
func setPlaceholderTextColorTo(color: UIColor)
{
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = color
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = color
}
}
像这样调用这些扩展方法:
searchBarTextField.setMagnifyingGlassColorTo(color: yourColor)
searchBarTextField.setClearButtonColorTo(color: yourColor)
searchBarTextField.setPlaceholderTextColorTo(color: yourColor)
var image: UIImage = UIImage(named: "search")!
self.searchBar.setImage(image, forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
自从 iOS 13 版本开始,我们就可以通过 SF Symbols 访问许多标准的系统图标。此外,如果你想为搜索栏设置统一的配色方案,以便它们看起来都相同,而不必重复编写代码,你可以使用 Appearance
工作流程一次性更改它们全部。例如:
let image = UIImage(systemName: "magnifyingglass")?.withTintColor(.white, renderingMode: .alwaysOriginal)
UISearchBar.appearance().setImage(image, for: .search, state: .normal)
这段代码将应用程序中的所有搜索栏都设置为使用标准放大镜图标,颜色为白色。您可以使用类似的代码来更改“清除”按钮图标、书签图标和结果列表图标。只需将.search
更改为适当的枚举值,并找到适当图标的系统名称(您可以从免费的SF System应用程序中获取)。
另一种使用Appearance
的方法是设置搜索视图中包含的所有视图的着色颜色,但在某些情况下可能会产生不想要的影响(即更改文本光标的颜色):
UIView.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .yellow
继续上面的回答,如果你想在storyboard中看到并进行更改,请先子类化UISearchBar并按照上述步骤操作。
但是请将更改添加到@IBInspectable变量中,不要忘记在类顶部设置@IBDesignable和在“Identity inspector”中设置搜索栏子类。
下面是Swift 3.0的完整工作子类和代码。在这里,您可以更改占位符文本、搜索文本和放大镜。
import UIKit
@IBDesignable
class CustomSearchBar: UISearchBar {
@IBInspectable var placeholderColor: UIColor? {
didSet {
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = placeholderColor
}
}
@IBInspectable var textColor: UIColor? {
didSet {
let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = textColor
}
}
@IBInspectable var magnifyingGlassColor: UIColor? {
didSet {
if let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField,
let glassIconView = textFieldInsideSearchBar.leftView as? UIImageView {
//Magnifying glass
glassIconView.image = glassIconView.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate)
glassIconView.tintColor = magnifyingGlassColor
} }
}
}