How to change the text color of a UISearchBarController UITextField

Discussion in 'iOS Programming' started by Dookieman, Dec 12, 2014.

  1. Dookieman macrumors 6502

    Joined:
    Oct 12, 2009
    #1
    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?
     
  2. JohnsonK macrumors regular

    Joined:
    Mar 6, 2014
    #2
    Are you by any chance, creating your UISearchBarController programmatically?
     
  3. Dookieman thread starter macrumors 6502

    Joined:
    Oct 12, 2009
  4. xStep macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #4
    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];
    }
    
     

Share This Page