ios - How to change UISearchBar Placeholder and image tint color? -
i've been trying search results hours, can't figured out. perhaps isn't possible. i'm trying change tint color of placeholder text , magnifying glass of uisearchbar. i'm targeting ios 8.0+ if matters. here's code , looks now:
let searchbar = uisearchbar() searchbar.placeholder = "search" searchbar.searchbarstyle = uisearchbarstyle.minimal searchbar.tintcolor = uicolor.whitecolor() 
i'd search , magnifying glass white, or perhaps dark green.
if have custom image use, can set image , change placeholder text color using similar following:
[searchbar setimage:[uiimage imagenamed:@"searchwhite"] forsearchbaricon:uisearchbariconsearch state:uicontrolstatenormal]; uitextfield *searchtextfield = [searchbar valueforkey:@"_searchfield"]; if ([searchtextfield respondstoselector:@selector(setattributedplaceholder:)]) { uicolor *color = [uicolor purplecolor]; [searchtextfield setattributedplaceholder:[[nsattributedstring alloc] initwithstring:@"search" attributes:@{nsforegroundcolorattributename: color}]]; } in example used purplecolor, instead can use + (uicolor *)colorwithred:(cgfloat)red green:(cgfloat)green blue:(cgfloat)blue alpha:(cgfloat)alpha method create custom dark green color.
edit: realized writing in swift... duh. typed out didn't leave answer in obj-c.
searchbar.setimage(uiimage(named: "searchwhite"), forsearchbaricon: uisearchbaricon.search, state: uicontrolstate.normal); var searchtextfield: uitextfield? = searchbar.valueforkey("searchfield") as? uitextfield if searchtextfield!.respondstoselector(selector("attributedplaceholder")) { var color = uicolor.purplecolor() let attributedict = [nsforegroundcolorattributename: uicolor.purplecolor()] searchtextfield!.attributedplaceholder = nsattributedstring(string: "search", attributes: attributedict) } swift 3.0
var searchtextfield: uitextfield? = searchbar.value(forkey: "searchfield") as? uitextfield if searchtextfield!.responds(to: #selector(getter: uitextfield.attributedplaceholder)) { let attributedict = [nsforegroundcolorattributename: uicolor.white] searchtextfield!.attributedplaceholder = nsattributedstring(string: "search", attributes: attributedict) }
Comments
Post a Comment