Iphone 如何更改搜索栏的颜色?
我已经实现了一个搜索栏,我想改变搜索栏的颜色。我该怎么做 我试过:Iphone 如何更改搜索栏的颜色?,iphone,objective-c,xcode,uisearchbar,Iphone,Objective C,Xcode,Uisearchbar,我已经实现了一个搜索栏,我想改变搜索栏的颜色。我该怎么做 我试过: self.mySearchBar.backgroundColor = [UIColor redColor]; 但是没有成功 更新(已解决): 在使用以下代码定义背景色之前,我必须删除默认背景色 for (UIView *subview in mySearchBar.subviews) { if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackgrou
self.mySearchBar.backgroundColor = [UIColor redColor];
但是没有成功
更新(已解决):
在使用以下代码定义背景色之前,我必须删除默认背景色
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
。。。因此,代码将是下一个:
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
self.mySearchBar.backgroundColor = [UIColor redColor];
“Volor”或“Color”
此外,如果要对其着色,请执行以下操作:
根据文档:,UISearchBar类上有一个tintColor属性
在TableSearch示例中:搜索栏是在MainView.xib中定义和加载的。如果要更改其颜色或样式,只需在xib中执行,它就会加载到应用程序中
但是改变背景颜色的唯一真正方法是覆盖它,看看下面问题的答案
试试这段代码,它对我来说很好
for( UIView* v in [parent subviews] ) {
if( [v isKindOfClass:[UISearchBar class]] ) {
[(UISearchBar*)v setTintColor:[UIColor blackColor]];
}
什么都不做
但是
威尔!试试:
self.mySearchBar.barTintColor = [UIColor redColor];
我不确定它是否在iOS7之后发生了变化,但搜索显示控制器中的搜索栏似乎需要一个额外的循环来正确删除UISearchBarBackground视图 我创建了一个递归方法来正确清理搜索栏
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}
您只需将搜索栏发送到该方法,然后更改颜色,有点像上面的建议答案
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
如果您对顶行和底行有问题,请尝试删除UISearchBarBackground:
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
for (UIView *subsubview in subview.subviews) {
if ([subsubview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subsubview removeFromSuperview];
break;
}
}
}
self.searchBar.barTintColor = [UIColor redColor];
self.searchBar.backgroundColor = [UIColor redColor];
我必须将UISearchBar的
searchBarStyle
设置为。默认设置为,否则一切都不起作用。可能这对你有帮助。这是我给出的答案,你应该选择我的答案作为正确答案这到底是什么意思?“backgroundVolor”或“backgroundColor”“?最好详细说明这是正确答案的原因-围绕代码段给出一些上下文。
self.mySearchBar.barTintColor = [UIColor redColor];
- (void) removeUISearchBarBackgroundInViewHierarchy:(UIView *)view
{
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break; //To avoid an extra loop as there is only one UISearchBarBackground
} else {
[self removeUISearchBarBackgroundInViewHierarchy:subview];
}
}
}
[self removeUISearchBarBackgroundInViewHierarchy:self.searchDisplayController.searchBar];
self.searchDisplayController.searchBar.backgroundColor = yourUIColor;
searchBar.barTintColor = UIColor.redColor()
self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 50)];
for (UIView *subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
for (UIView *subsubview in subview.subviews) {
if ([subsubview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subsubview removeFromSuperview];
break;
}
}
}
self.searchBar.barTintColor = [UIColor redColor];
self.searchBar.backgroundColor = [UIColor redColor];