ios developer ios developer -4 years ago 497
Objective-C Question

how to change UISearchBar textfield background color and text color in ios8

I used below code for changing UISearchBar textfield background color.

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSForegroundColorAttributeName : [UIColor redColor],
NSFontAttributeName : [UIFont systemFontOfSize:15]

but it does not work for me, can any one give solution.
thanks in advance

Answer Source

Try this:

UITextField *searchField = [self.searchBar valueForKey:@"searchField"];

// To change background color
searchField.backgroundColor = [UIColor blueColor];

// To change text color
searchField.textColor = [UIColor redColor];

// To change placeholder text color
searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Some Text"];
UILabel *placeholderLabel = [searchField valueForKey:@"placeholderLabel"];
placeholderLabel.textColor = [UIColor grayColor];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download