0

I am specifically looking for an iOS-7 answer. If I had an image, the answer would be

[mySearchBar setSearchFieldBackgroundImage:myImage forState:UIControlStateNormal];

But I just want to apply a color. Is there a simple way of doing this with just a color? Again, for iOS-7.

Note: I see a host of very complicated answers here on SO: with traversing, etc. I hoping for something simple and that won't cause my app to be rejected.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
learner
  • 11,490
  • 26
  • 97
  • 169

2 Answers2

0

If I understand correctly, you are looking for the barTintColor property. You can set the barTintColor property as such,

mySearchBar.barTintColor = [UIColor blackColor];

or

[mySearchBar setBarTintColor:[UIColor blackColor];

Hope that helps!

  • no that didn't do it. I am referring to the very inner background: the background of the search icon: the background that would hold the text. See the following post for reference: http://stackoverflow.com/questions/13817330/how-to-change-inside-background-color-of-uisearchbar-component-on-ios. There are many such posts. But many of their answers involve iterating through view hierarchies or using private API; which is why they don't work for me. – learner Jul 30 '14 at 22:56
0

First you need to find the UITextField inside the search bar and then change its color. Here is the code to get the text field:

for (UIView *subView in searchBar.subviews)
    {
        for (UIView *secondLevelSubview in subView.subviews){
            if ([secondLevelSubview isKindOfClass:[UITextField class]])
            {
                UITextField *searchBarTextField = (UITextField *)secondLevelSubview;

                return searchBarTextField;
            }
        }
    }
    return nil;
sahara108
  • 2,829
  • 1
  • 22
  • 41