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条回答
Evening l夕情丶
2楼-- · 2019-04-20 17:36

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.

查看更多
Animai°情兽
3楼-- · 2019-04-20 17:36

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

查看更多
欢心
4楼-- · 2019-04-20 17:42

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];
}
查看更多
beautiful°
5楼-- · 2019-04-20 17:43

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

查看更多
兄弟一词,经得起流年.
6楼-- · 2019-04-20 17:44

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