UISearchBar text color

2019-04-20 17:42发布

问题:

Browsed the documentation and I couldn't find anything to change the color of UISearchBar. Does anybody know how to change it? There isn't any textColor property :/

Thx

回答1:

You can do the following: Just get the searchField property from the SearchBar, and then change its textColor property.

UITextField *searchField = [searchbar valueForKey:@"_searchField"];
searchField.textColor = [UIColor redColor]; //You can put any color here.

That's it! Now you manipulate the textField in any way possible.



回答2:

iOS 8: See https://stackoverflow.com/a/28183058/308315

iOS 6 / 7:

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


回答3:

Works on iOS 7 and later:

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

You may remove unused attribute as well.

UPDATE. Due to appearanceWhenContainedIn is deprecated in iOS 9, see the Dishant's answer below: https://stackoverflow.com/a/38893352/2799722



回答4:

I suspect you could use techniques described in this post

Modifying the code presented there slightly, you subclass UISearchBar:

@interface SearchBar : UISearchBar {
}
@end

Then in your implementation:

- (void)layoutSubviews {
    UITextField *searchField;
    NSUInteger numViews = [self.subviews count];
    for(int i = 0; i < numViews; i++) {
        if([[self.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]) {
            searchField = [self.subviews objectAtIndex:i];
        }
    }
    if(!(searchField == nil)) {
        searchField.textColor = [UIColor redColor];
    }

    [super layoutSubviews];
}

I haven't tested either the original post's code or this code, but looks like it ought to work. -wkw



回答5:

Here's a category that adds this functionality:

@implementation UISearchBar (UISearchBar_TextColor)

- (UITextField *)field {
    // HACK: This may not work in future iOS versions
    for (UIView *subview in self.subviews) {
        if ([subview isKindOfClass:[UITextField class]]) {
            return (UITextField *)subview;
        }
    }
    return nil;
}

- (UIColor *)textColor {
    return self.field.textColor;
}

- (void)setTextColor:(UIColor *)color {
    self.field.textColor = color;
}

@end


回答6:

For iOS11, I found this worked:

After setting the searchController into the navigationItem, the search text was black on black. To make it white, I had to do:

searchController.searchBar.barStyle = .blackTranslucent

It was the only thing that worked for me. My app has a transparent navigation bar to let the background gradient show through, and I am guessing the SearchBar takes on that appearance since my appearance settings for UISearchBar were largely ignored with one exception:

UISearchBar.appearance().tintColor = UIColor.red

This made the Cancel button and the text insertion cursor red. The placeholder text was light gray.

Note that: UISearchBar.appearance().barStyle = .blackTranslucent did not work - it had to be set on the instance. This also had no visible effect on the search bar (it was still transparent like the navigation bar); it just made the search text white.



回答7:

appearanceWhenContainedIn is deprecated in iOS 9 , so we have to use below method for iOS 9 and above.

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
 setTintColor:[UIColor whiteColor]];


回答8:

With iOS 11 the search bar is expected to become part of the navigation bar which, as you might expect, adds all kinds of new "features."

I think it's a bug but I found that I needed to do the following to change the text (and cancel button) colour:

self.searchController.searchBar.barStyle = UISearchBarStyleMinimal;
[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]]
     setTintColor:[UIColor whiteColor]];

I found that the bar-style, when left to "default," would make the text black no matter the tint colour, etc. When set to either Minimal or Prominent the text was visible.



回答9:

Here's a cleaner approach:

UITextField *searchField = nil;

for (UIView *v in self.searchBar.subviews)
{
    if ([v isKindOfClass:[UITextField class]])
    {
        searchField = (UITextField *)v;
        break;
    }
}

if (searchField)
{
    searchField.textColor = [UIColor whiteColor];
}


回答10:

Modified the category suggested by David Foster (@david-foster) to work on iOS 8.

static UITextField *PBFindTextFieldInView(UIView *view) {
    for(UIView *subview in view.subviews) {
        if([subview isKindOfClass:UITextField.class]) {
            return (UITextField *)subview;
        } else {
            UITextField* textField = PBFindTextFieldInView(subview);
            if(textField) {
                return textField;
            }
        }
    }
    return nil;
}

@implementation UISearchBar (Appearance)

- (UITextField *)field {
    return PBFindTextFieldInView(self);
}

- (UIColor *)textColor {
    return self.field.textColor;
}

- (void)setTextColor:(UIColor *)color {
    self.field.textColor = color;
}

@end


回答11:

After setting the searchController in the navigationItem for iOS 11, I found that attempting to set the textColor via UIAppearance for any UITextField within a UISearchBar had no affect, but a custom appearance property that simply called the regular textColor worked just fine.

// Implement a custom appearance property via a UITextField category
@interface UITextField (SearchBarTextColor)

@property (nonatomic, strong) UIColor * textColorWorkaround UI_APPEARANCE_SELECTOR;

@end

@implementation UITextField (SearchBarTextColor)

- (UIColor *)textColorWorkaround {
    return self.textColor;
}

- (void)setTextColorWorkaround:(UIColor *)textColor {
    self.textColor = textColor;
}

@end

And then use as follows:

UITextField *textFieldProxy = [UITextField appearanceWhenContainedInInstancesOfClasses:@[UISearchBar.class]];
textFieldProxy.textColorWorkaround = UIColor.lightGrayColor;

P.S. The same trick helped me color the seemingly inaccessible labels of UIDatePicker and UIPickerView