Changing UISearchBar search field background
ios, objective-c, uisearchbar
Solution
you can use the appearanceWhenContainedIn to target elements without traversing the view hierarchy. Something like:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor lightGrayColor]];
UPDATE:
As `appearanceWhenContainedIn` is now deprecated, use
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar self]]] <your setter here>];
Problem
I have a `UISearchBar` contained within a `UIView` and the `UIView` has been assigned to a `UITableView's tableHeaderView.` I'm trying to style the `UISearchBar` to look like this: Out of the gate, this is what it looks like: There are loads of SO questions and answers on how to style `UISearchBar`, its search field, etc., some with particular attention to backgrounds. Many of them involve traversing the `UISearchBar's subviews`, finding a `UISearchBarBackground` (or `_UISearchBarSearchFieldBackgroundView`) object, and setting its background color directly. I'm trying to find a supported API for doing this, however... The `setSearchFieldBackgroundImage:forState:` method seems to have great potential, but this is what I get when I use a 1px white (or transparent) image using `UIControlStateNormal`: Any ideas as to how to get this to work? I'd appreciate someone pointing me to a SO post that answers this, but I really do think I've read them all. Thanks a ton.