How can I change the UISearchBar search text color?

MrTourkos picture MrTourkos · Feb 13, 2015 · Viewed 55k times · Source

How can I change the text color of a UISearchBar?

Answer

Christian picture Christian · Feb 13, 2015

You have to access the UITextField inside the UISearchBar. You can do that by using valueForKey("searchField")

var textFieldInsideSearchBar = yourSearchbar.valueForKey("searchField") as? UITextField

textFieldInsideSearchBar?.textColor = yourcolor

Swift 3 update

let textFieldInsideSearchBar = yourSearchbar.value(forKey: "searchField") as? UITextField

textFieldInsideSearchBar?.textColor = yourcolor