如何在iOS7中定制UISearchBar颜色?

如何在iOS7中定制UISearchBar颜色?,ios7,uisearchbar,Ios7,Uisearchbar,我找了不少,但找不到一个好答案 我想更改内部圆形视图的背景色 就像搜索点击上的Tweetbot一样,它从灰色变为蓝色 我知道我可能需要迭代子视图,但我不知道如何获得正确的对象。(背景色不是_searchLabel) 此元素的默认对比度非常差,甚至一点都不好笑:(使用此代码 _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.contentView.bounds.size.width, 44.0f)

我找了不少,但找不到一个好答案

我想更改内部圆形视图的背景色

就像搜索点击上的Tweetbot一样,它从灰色变为蓝色

我知道我可能需要迭代子视图,但我不知道如何获得正确的对象。(背景色不是_searchLabel)

此元素的默认对比度非常差,甚至一点都不好笑:(

使用此代码

_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.contentView.bounds.size.width, 44.0f)];
_searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
_searchBar.delegate         = self;
[self.view addSubView:_searchBar];

UITextField *txfSearchField = [_searchBar valueForKey:@"_searchField"];
txfSearchField.backgroundColor = [UIColor redColor];

好的,这是可行的。但是请注意,您不能预先设置UIBarStyle,否则它将覆盖所有内容。

如果您仍在寻找更好的答案,我只是偶然发现了这条线索,并找到了一个很好的解决方案:

如果您查看Sandeep Systematix给出的答案(不是公认的答案,而是下面的答案),他提到了一种使用此方法修改任何类中的子视图的非常干净的方法:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
您可以在苹果的文档中了解更多信息:

也就是说,您需要在UISearchBar中更改UITextField的白色圆形背景:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor redColor]];
现在,如果您需要创建具有不同样式的不同UISearchBar,只需创建UISearchBar的子类,您将得到如下结果:

[[UITextField appearanceWhenContainedIn:[MyCustomSearchBar class], nil] setBackgroundColor:[UIColor redColor]];

这有帮助吗?没有:(我不想添加背景图像。只需更改内部圆形视图的颜色。我的意思是,解决方案2的答案位于该解决方案下方,您的答案是什么?它们都只更改背景图像?!这将导致应用商店拒绝。