Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Dookieman

macrumors 6502
Original poster
Oct 12, 2009
390
67
Hey everyone,

I've been looking all over the internet to find a solution to this problem and everything I find doesn't work for me.

I want to change the text color of the UITextField and placeHolderLabel in a UISearchController to white since I have a dark themed application and the standard black text color is difficult to read.

Currently, for testing purposes I am changing the text color via the private call "_searchField" and "_placeholderlabel" changing it that way, but I've read that Apple may reject the application if they catch you doing that. The other suggestion that I have some by is this:

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

Which would go in my AppDelegate.m file. This does not work for me. Maybe I'm doing something wrong?

Anyone else have a solution to this?
 

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
I played with this a little and found that you have to set the appearance later than the AppDelegate and if you change the barStyle or searchBarStyle then you have to set the appearance after the change.

Review the following test code;

Code:
- (void)viewDidLoad {
    [super viewDidLoad];

    mySearchBar = [[UISearchBar alloc] initWithFrame: CGRectMake(100, 100, 400, 70)];
    mySearchBar.delegate = (id<UISearchBarDelegate>) self;
    mySearchBar.prompt = @"Prompt";
    mySearchBar.placeholder = @"Type search value here";
    
[COLOR="red"]    mySearchBar.barStyle = UIBarStyleBlackTranslucent;
    [COLOR="SeaGreen"]// Must come AFTER setting the barStyle which seems to reset these settings.[/COLOR]
    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor yellowColor]];
[/COLOR]
[COLOR="Red"]    mySearchBar.searchBarStyle = UISearchBarStyleDefault;
    [COLOR="SeaGreen"]// Must come AFTER setting the searchBarStyle which seems to reset these settings.[/COLOR]
    [[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
[/COLOR]    
    [self.view addSubview: mySearchBar];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.