【发布时间】:2016-06-14 07:35:20
【问题描述】:
目前,我将默认放大镜作为我的搜索栏图标。但是,我想在其位置放置一个自定义图像,尤其是这个图像:
自定义箭头图标
如何将搜索栏默认图标更改为自定义图像?
【问题讨论】:
标签: ios swift icons customization uisearchbar
目前,我将默认放大镜作为我的搜索栏图标。但是,我想在其位置放置一个自定义图像,尤其是这个图像:
自定义箭头图标
如何将搜索栏默认图标更改为自定义图像?
【问题讨论】:
标签: ios swift icons customization uisearchbar
你可以使用setImage函数
searchBar.setImage(UIImage(named: "your image"), forSearchBarIcon: .Search, state: .Normal)
Swift3
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
【讨论】:
方式一:
作为luiyzhengstrong>的回答,你可以改变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()
输出:
搜索图标图像色调颜色变化
【讨论】:
imageV.image = imageV.image?.withRenderingMode(UIImage.RenderingMode.alwaysTemplate)
luiyzhengstrong>的回答效果很好。 它的 Swift 3 版本是:
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)
【讨论】: