我想将搜索栏的背景设为清晰或外观的导航栏颜色
目前正在显示
如何使背景清除serach bar?
答案 0 :(得分:4)
因为UI SearchBar有自己的背景视图,它是黑色的,如果我们删除该视图,那么UISearchbar的背景将变得清晰:
for (UIView *subview in mySearchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
break;
}
}
答案 1 :(得分:2)
//搜索栏(使用背景图片)
UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(568, 30, 200, 44)];
UIImageView* iview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"blue.png"]];
iview.frame = CGRectMake(0, 0, 200, 44);
[bar insertSubview:iview atIndex:1];
[iview release];
[self.view addSubview:bar];
[bar release];
或(使用颜色)
//搜索栏
UISearchBar* bar = [[UISearchBar alloc] initWithFrame:CGRectMake(568, 30, 200, 44)];
[[[bar subviews] objectAtIndex:0] setAlpha:0.0];
bar.tintColor = [UIColor colorWithRed:.376f green:.386f blue:.452f alpha:1.0];
bar.clipsToBounds = YES;
[self.view addSubview:bar];
[bar release];
答案 2 :(得分:2)
按如下方式设置UISearchBar的背景图像。
[searchBar setBackgroundImage:[UIImage new]];
现在UISearchBar背景颜色将消失。也适用于iOS 7.1
答案 3 :(得分:0)
清除背景颜色(如果要设置图像,请使用注释代码)
for (UIView *subview in searchBar.subviews)
{
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
// if You want set iamge then use comment code
/* UIView *backgroundView = [[UIView alloc] initWithFrame:subview.frame];
bg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"searchBar"]];
[self.searchBar insertSubview:backgroundView aboveSubview:subview];*/
[subview removeFromSuperview];
}
}
答案 4 :(得分:0)
清除搜索栏的背景视图:
[[[self.searchBar subviews] objectAtIndex:0] setHidden:YES];
for (id subview in self.searchBar.subviews) {
if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[subview removeFromSuperview];
}
}
答案 5 :(得分:0)
我在iOS8中发现,这可以更好地将UISearchBar简化为文本字段。这将允许您摆脱背景颜色。
for (UIView *subView in self.searchBar.subviews) {
for (UIView *secondLevelSubview in subView.subviews) {
if (![secondLevelSubview isKindOfClass:NSClassFromString(@"UISearchBarTextField")]) {
[secondLevelSubview removeFromSuperview];
}
}
}
答案 6 :(得分:-1)
尝试使用此行:
[[ [searchBar subviews] objectAtIndex:0] removeFromSuperview];
[[ [searchBar subviews] objectAtIndex:0] setBackgroundColor:[UIColor yellowColor]];
当然可以帮助你。