如何将UISearchBar图标更改为自定义图像?
问题描述:
目前,我有默认的放大镜作为我的搜索栏图标。然而,我想把一个自定义图像放在它的位置,特别是这个图像:Custom Arrow Icon如何将UISearchBar图标更改为自定义图像?
我该如何去改变搜索栏默认图标到自定义图像?
答
可以使用setImage
功能
searchBar.setImage(UIImage(named: "your image"), forSearchBarIcon: .Search, state: .Normal)
答
方法1:
由于luiyezheng的回答,您可以更改的UISearchBar图标的图像。
UISearchBar.appearance().setImage(UIImage(named: "new_search_icon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
方式2:
你可以改变的UISearchBar
图标搜索图标的色调颜色。
let textFieldInsideSearchBar = searchBarCustom.valueForKey("searchField") as? UITextField
let imageV = textFieldInsideSearchBar?.leftView as! UIImageView
imageV.image = imageV.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
imageV.tintColor = UIColor.purpleColor()
输出:
答
luiyezheng的答案的伟大工程。它的Swift 3版本是:
searchBar.setImage(UIImage(named: "Image Name"), for: .search, state: .normal)