UISearchBar text color

2019-04-20 17:03发布

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

11条回答
冷血范
2楼-- · 2019-04-20 17:24

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.

查看更多
\"骚年 ilove
3楼-- · 2019-04-20 17:25

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

iOS 6 / 7:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
查看更多
Root(大扎)
4楼-- · 2019-04-20 17:26

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.

查看更多
\"骚年 ilove
5楼-- · 2019-04-20 17:30

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
查看更多
SAY GOODBYE
6楼-- · 2019-04-20 17:31

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

查看更多
祖国的老花朵
7楼-- · 2019-04-20 17:35

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
查看更多
登录 后发表回答