您如何在iOS7中自定义UISearchbar颜色



我搜索了很多,但找不到一个很好的答案。

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

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

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

此元素的默认对比太糟糕了,甚至都不有趣:(

好,这有效。但是请注意,您无法事先设置UibarStyle,否则它将覆盖所有内容。https://stackoverflow.com/a/19836215/1252720

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

中的Uisearchbar文本颜色更改

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

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

您可以在Apple的文档中阅读有关此信息的更多信息:https://developer.apple.com/library/ios/documentation/uikit/reference/reiapearance/uiappearance_protocol/referencole/reference/Reference/Reference.html

也就

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

现在,如果您需要创建具有不同样式的不同uisearchbars,则只需创建一个Uisearchbar的子类,最终会得到类似的东西:

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

使用此代码。

_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];

最新更新