如何更改UISearchBar图标的颜色?

我不知道如何修改原始的searchIcon的颜色。

在此处输入图像描述

您可以使用白色搜索图标的自定义图像,因为搜索栏不会修改您提供的图像。 要设置自定义图像,请使用此方法。 ( 链接到文档。 )

 - (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) 

我找到的最佳解决方案是在UISearchBar中更改UItextField中图标的颜色

(此解决方案使用原始的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 } 

这个灵魂在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) 

按照费德里卡的说法……快速做到这一点

 var image: UIImage = UIImage(named: "search")! self.searchBar.setImage(image, forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal) 

斯威夫特-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) 

继这里的一些答案之后

如果您想在故事板中查看并进行更改,请先在子类UISearch栏中进行更改,然后执行上述操作。

但是,在@IBInspectable变量中添加更改并且不要忘记类顶部的@IBDesignable并在“Identity inspector”中设置searchbar子类

我在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 } } } 

}

确保在使用setImage API时,使用UIImageRenderingModeAlwaysTemplate渲染模式传递图像。 例如:

 [self.searchBar setImage:[[UIImage imageNamed: @"icon-search"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

只需更改搜索图标的颜色,而无需更改Swift 3中的实际图标:

 if let textField = self.searchBar.value(forKey: "searchField") as? UITextField, let iconView = textField.leftView as? UIImageView { iconView.image = iconView.image?.withRenderingMode(UIImageRenderingMode.alwaysTemplate) iconView.tintColor = UIColor.red } 

像这样创建一个结果:

改变了搜索栏图标

对于迅捷3

  searchBar.setImage(UIImage(named: "location-icon-black"), for: .search, state: .normal) 
 if(IOS_7) { self.searchBar.searchBarStyle = UISearchBarStyleMinimal; self.searchBar.backgroundImage = [UIImage imageWithColor:[UIColor redColor] cornerRadius:5.0f]; }