本文介绍了UISearchBar:更改输入字段的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改UISearchBar输入字段的背景颜色。这是您输入要搜索的文本的圆角视图,默认颜色为白色。我想将其更改为灰色

I'm trying to change the background color of input field of UISearchBar.It's the rounded view where you input text to search, the default color is white. I would like to change it to gray

我试过:

for (UIView *subView in searchBar.subviews) {
    if ([subView isKindOfClass:NSClassFromString(@"UITextField")]) {
        UITextField *textField = (UITextField *)subView;
        [textField setBackgroundColor:[UIColor grayColor]];
    }

但它不起作用:(

我还试图将图像视图插入到TextField中,但看起来圆角视图与TextField是分开的。那么,任何线索?

I also tried to insert an image view to TextField but it seems the rounded view is separate from TextField. So, any clues?

推荐答案

=)

for (UIView *subView in _searchBar.subviews) {
    for(id field in subView.subviews){
        if ([field isKindOfClass:[UITextField class]]) {
            UITextField *textField = (UITextField *)field;
            [textField setBackgroundColor:[UIColor grayColor]];
        }
    }
}

这篇关于UISearchBar:更改输入字段的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 23:24