How to change UISearchBar Icon to custom image?

萝らか妹 提交于 2019-12-10 02:33:41

问题


Currently, I have the default magnifying glass as my search bar icon. However, I want to put a custom image in its place, particularly this image:

Custom Arrow Icon

How would I go about changing the search bar default icon to the custom image?


回答1:


you can use setImage function

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

Swift3

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



回答2:


Way 1 :

As luiyezheng answer, You can change the image of UISearchBar iCon.

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

Search Icon image Change

Way 2 :

you can change tint color of Search iCon of 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()

Output :

Search Icon image tint color change




回答3:


luiyezheng's answer works great. The Swift 3 version of it is:

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


来源:https://stackoverflow.com/questions/37805754/how-to-change-uisearchbar-icon-to-custom-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!