我使用以下代码在UINavigationItem中添加了一个搜索栏:
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectZero];
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.title = self.category.title;
[searchBar release];但结果UI如下所示:

如何更改搜索栏的颜色并使其与导航栏的背景相同?
谢谢。
发布于 2011-08-09 11:11:10
对于UINavigationBar,有多种方法可以操纵它的背景色:
对于搜索栏,tintColor也是可用的。但在你的例子中,我建议继承UISearchBar,并提供如下所示的实现,以完全摆脱后台绘制例程。在这种情况下,你会在一个透明的工具栏上看到搜索栏,并且你只需要管理UINavigationBar的颜色。
//TransparentSearchBar.m
@interface TransparentSearchBar()
- (void)removeBackgroundView;
@end
@implementation TransparentSearchBar
- (id)init
{
self = [super init];
if(self) {
[self removeBackgroundView];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if(self) {
[self removeBackgroundView];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self) {
[self removeBackgroundView];
}
return self;
}
- (void)removeBackgroundView
{
for (UIView *view in self.subviews)
{
if ([view isKindOfClass:NSClassFromString
(@"UISearchBarBackground")])
{
[view removeFromSuperview];
break;
}
}
}
@end发布于 2011-08-09 11:10:18
如果你看到关于UISearchBar的文档,它的属性是"tintColor“。将其设置为导航栏的颜色。
如果您不知道导航栏的颜色,请获取navigationbar的tintColor。
你可能还想使用搜索栏的“半透明”属性。可能会更好用。
https://stackoverflow.com/questions/6990950
复制相似问题