UISearchBar change placeholder color UISearchBar change placeholder color ios ios

UISearchBar change placeholder color


for iOS5+ use the appearance proxy

[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];


Found the answer from Change UITextField's placeholder text color programmatically

// Get the instance of the UITextField of the search barUITextField *searchField = [searchBar valueForKey:@"_searchField"];// Change search bar text colorsearchField.textColor = [UIColor redColor];// Change the search bar placeholder text color[searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];


First solution is OK, but if you use multiple UISearchBar, or create a lot of instances it may fail. The one solution that always work for me is to use also appearance proxy but directly on UITextField

   NSDictionary *placeholderAttributes = @{                                            NSForegroundColorAttributeName: [UIColor darkButtonColor],                                            NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15],                                            };    NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.searchBar.placeholder                                                                                attributes:placeholderAttributes];    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setAttributedPlaceholder:attributedPlaceholder];