Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change UISearchBar Icon to custom image?

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

Screen Shot

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

like image 875
Kolby O'Malley Avatar asked Jun 14 '16 07:06

Kolby O'Malley


2 Answers

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)
like image 119
ilovecomputer Avatar answered Sep 27 '22 20:09

ilovecomputer


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

screen shot

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

screen shot

like image 28
iOSDevCenter Avatar answered Sep 27 '22 18:09

iOSDevCenter