如何将UISearchBar图标更改为自定义图像?

Kol*_*ley 9 customization icons uisearchbar ios swift

目前,我有默认的放大镜作为我的搜索栏图标.但是,我想在其位置放置自定义图像,尤其是此图像:

自定义箭头图标

屏幕截图

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

小智 14

方式1:

作为luiyezheng的回答,你可以改变UISearchBar iCon的形象.

UISearchBar.appearance().setImage(UIImage(named: "new_search_icon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal)
Run Code Online (Sandbox Code Playgroud)

搜索图标图像更改

屏幕截图

方式2:

你可以改变iCon的搜索iCon的色调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()
Run Code Online (Sandbox Code Playgroud)

输出:

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

屏幕截图


bil*_*p22 12

你可以使用setImage功能

 searchBar.setImage(UIImage(named: "your image"), forSearchBarIcon: .Search, state: .Normal)
Run Code Online (Sandbox Code Playgroud)

Swift3

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