如何更改UISearchBar图标的颜色?

Dam*_*ito 24 objective-c uisearchbar ios

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

在此输入图像描述

Mil*_*der 37

searchIcon 位于 searchTextField 的 leftView 中。由于这是可访问的,我们可以轻松地为视图设置 tintColor。tintColor 也用于 searchIcon。

因此,在您的代码中,您可以使用以下代码将搜索图标更改为白色:

searchBar.searchTextField.leftView?.tintColor = .white
Run Code Online (Sandbox Code Playgroud)


raf*_*raf 30

我找到的最佳解决方案是在 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
    }
Run Code Online (Sandbox Code Playgroud)


小智 27

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

- (void)setImage:(UIImage *)iconImage
forSearchBarIcon:(UISearchBarIcon)icon
           state:(UIControlState)state;
Run Code Online (Sandbox Code Playgroud)

示例代码:

[searchBar setImage:[UIImage imageNamed:@"SearchIcon"]
   forSearchBarIcon:UISearchBarIconSearch
              state:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

在Swift中:

searchBar.setImage(UIImage(named: "SearchIcon"), for: .search, state: .normal)
Run Code Online (Sandbox Code Playgroud)


Dar*_*der 8

这个灵魂在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
    }
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)
searchController.searchBar.setMagnifyingGlassColorTo(color: mainColor)
Run Code Online (Sandbox Code Playgroud)


All*_*ome 6

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

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


Ben*_*wry 6

仅更改搜索图标的颜色,而不更改 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
}
Run Code Online (Sandbox Code Playgroud)

创建如下结果:

改变的搜索栏图标


小智 5

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
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样调用这些扩展方法:

searchBarTextField.setMagnifyingGlassColorTo(color: yourColor)
searchBarTextField.setClearButtonColorTo(color: yourColor)
searchBarTextField.setPlaceholderTextColorTo(color: yourColor)
Run Code Online (Sandbox Code Playgroud)


Boc*_*ica 5

适用于使用界面构建器的人的解决方案。您可以向 UISearchBar 添加用户定义的运行时属性:

searchField.leftView.tintColor类型Color 您可以选择所需的图标颜色,可以从您的颜色资源库中选择,以支持浅色和深色模式的不同颜色。

在此处输入图片说明