I have a UISearchBar with a dark background color so I was trying to change the place holder text color of UISearchBar (which will be gray by default) but I didn't find a way to set it. So I thought of getting some help :) please suggest me how this can be achieved thanks in advance :)
-
The easiest way is trying to use textfield instead of the native searchBar, it's very limited scope. – Frans Raharja Kurniawan Sep 09 '14 at 14:19
-
Looks like this is answered here: http://stackoverflow.com/questions/11827585/uisearchbar-change-placeholder-colour – terrafirma9 Sep 09 '14 at 14:19
-
@terrafirma9 actually that sets the appearance for the textfield which is one of the subviews of UISearchBar. But it doesnt solve my placeholder problem. BTW I'm using that appearance proxy too to change the color of the textfield's text color. – iamyogish Sep 09 '14 at 14:22
-
well, it definitely changed placeholder appearance for me, but you're right that it would probably affect the textfield. you could try changing color based on control state? can't think of another way w/o subclassing or abandoning searchbar for textfield. – terrafirma9 Sep 09 '14 at 14:36
2 Answers
Note: At the time I wrote this answer I was working on iOS 7 & this workaround worked on iOS 8 as well. It may not work on iOS 9.
Ok it's been two days since I posted this question. Though I din't get the answer I got a workaround for this problem.
Note : I am using storyboard for my application & I have subclassed the UISearchBar and this workaround working like a gem for me.
First and foremost add an appearance proxy to UILabel when its contained in the UISearch bar class like this :
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor whiteColor]];//change the color to whichever color needed
Then the most important thing is If you're using storyboard you must and should write some text in the placeholder field of the attribute inspector like this
If you forget to write something in the placeholder field, definitely this trick will not work when you run the application and the placeholder text will look in usual gray color.
Once you're done with writing some text in placeholder field, set the place holder text in your UISearchBar sub class like this :
self.placeholder = @"My Placeholder text";
Now run the application !!! You'll get the place holder text in the color which you have set in appdelegate:) Hope this helps someone :)

- 2,372
- 2
- 23
- 40
-
-
Hi @RobRingham Thanks for bringing this into notice. At the time of writing, I was using iOS 7 & it worked on iOS 8 as well. I will make an edit saying this is tried and tested in iOS 7 & 8. – iamyogish Jul 21 '16 at 14:44
Here is the solution
[searchBar setValue:[UIColor blueColor] forKeyPath:@"_searchField.textColor"];

- 5,318
- 3
- 25
- 51