【问题标题】:How to change UISearchBar Icon to custom image?如何将 UISearchBar 图标更改为自定义图像?
【发布时间】:2016-06-14 07:35:20
【问题描述】:

目前,我将默认放大镜作为我的搜索栏图标。但是,我想在其位置放置一个自定义图像,尤其是这个图像:

自定义箭头图标

如何将搜索栏默认图标更改为自定义图像?

【问题讨论】:

    标签: ios swift icons customization uisearchbar


    【解决方案1】:

    你可以使用setImage函数

     searchBar.setImage(UIImage(named: "your image"), forSearchBarIcon: .Search, state: .Normal)
    

    Swift3

    searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
    

    【讨论】:

      【解决方案2】:

      方式一:

      作为luiyzheng​​strong>的回答,你可以改变UISearchBar图标的图像。

      UISearchBar.appearance().setImage(UIImage(named: "new_search_icon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
      

      搜索图标图片更改

      方式 2:

      您可以更改UISearchBar iCon 的搜索图标的色调颜色。

      let textFieldInsideSearchBar = searchBarCustom.valueForKey("searchField") as? UITextField
      let imageV = textFieldInsideSearchBar?.leftView as! UIImageView
      imageV.image = imageV.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
      imageV.tintColor = UIColor.purpleColor()
      

      输出:

      搜索图标图像色调颜色变化

      【讨论】:

      • 使用 Swift 4.2:imageV.image = imageV.image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
      【解决方案3】:

      luiyzheng​​strong>的回答效果很好。 它的 Swift 3 版本是:

      searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多