0

I have searched quite a bit but cannot find a good answer to this.

I want to change the backgroundColor of the inner rounded view.

Like in Tweetbot on the search tap where it changes from gray to blue.

I understand that I probably need to iterate over the subviews but I don't know how to get the right object. (for the backgroundColor it's not the _searchLabel)

The default contrast of this element is so bad it's not even funny :(

1b0t
  • 432
  • 1
  • 5
  • 11

3 Answers3

0

Ok, this works. But note that you can't set a UIBarStyle beforehand or it will override everything. https://stackoverflow.com/a/19836215/1252720

Community
  • 1
  • 1
1b0t
  • 432
  • 1
  • 5
  • 11
0

If you're still looking for a better answer, I just stumbled across this thread and found a great solution: UISearchBar text color change in iOS 7

If you look at the answer given by Sandeep-Systematix (not the accepted answer, but the answer right below), he mentions a really clean way to modify subviews in any class with this method:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];

You can read more about this in Apple's documentation: https://developer.apple.com/library/ios/documentation/uikit/reference/UIAppearance_Protocol/Reference/Reference.html

That said, here's what you'll need to change the white, rounded background of the UITextField inside the UISearchBar:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor redColor]];

Now if you needed to create different UISearchBars with different styles, you would simply create a subclass of UISearchBar and you'd end up with something like this:

[[UITextField appearanceWhenContainedIn:[MyCustomSearchBar class], nil] setBackgroundColor:[UIColor redColor]];
Community
  • 1
  • 1
iamceege
  • 11
  • 4
-1

Use this code.

_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.contentView.bounds.size.width, 44.0f)];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate         = self;
[self.view addSubView:_searchBar];

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];
Balram Tiwari
  • 5,657
  • 2
  • 23
  • 41