I could not get it to work with any of the above solutions.
I created the following UISearchBar category that works correctly on iOS 8.4 and 10.3 :
UISearchBar + PlaceholderColor.h
UISearchBar + PlaceholderColor.m
#import "UISearchBar+PlaceholderColor.h" @implementation UISearchBar (PlaceholderColor) - (void)setPlaceholderColor:(UIColor *)placeholderColor { UILabel *labelView = [self searchBarTextFieldLabelFromView:self]; [labelView setTextColor:placeholderColor]; } - (UILabel *)searchBarTextFieldLabelFromView:(UIView *)view { for (UIView *v in [view subviews]) { if ([v isKindOfClass:[UILabel class]]) { return (UILabel *)v; } UIView *labelView = [self searchBarTextFieldLabelFromView:v]; if (labelView) { return (UILabel *)labelView; } } return nil; } @end
USING
[mySearchBar setPlaceholderColor:[UIColor redColor]]
IMPORTANT NOTE:
Make sure you call setPlaceholderColor: AFTER your UISearchBar is added to the view and creates its own view hierarchy.
If you open the search panel programmatically, name it AFTER your call to startFirstResponder, as such:
[mySearchBar becomeFirstResponder]; [searchBar setPlaceholderColor:[UIColor redColor]];
Otherwise, if you use UISearchBarDelegate :
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { [searchBar setPlaceholderColor:[UIColor redColor]]; }
m_katsifarakis Jun 02 '17 at 12:39 on 2017-06-02 12:39
source share