2012-01-12 108 views
0

以下隐藏UIsearchBar背景的代码可以正常工作,直到iOs4.2,但不在iOS4.3或更高版本中。隐藏iOS5中的UISearchBar背景

for (UIView *subview in searchBar.subviews) { 
    if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
     [subview removeFromSuperview]; 
     break; 
    } 
} 
+0

由于外观API,视图层次可能已更改。在'-viewDidLoad'中设置一个断点,并在程序暂停时,在控制台中输入'po [searchBar recursiveDescription]'来获取整个视图层次结构的列表,包括私有类名称。 – 2012-01-12 18:33:22

+0

@Mark Adams - 不知道,但以编程方式创建UIsearchBar,而不是nib为我工作 – iOSPawan 2012-01-12 18:57:41

+1

如果您正在执行'-initWithFrame:'中的代码,那很明显。从nib加载使用'-initWithCoder:'初始化程序。 – 2012-01-12 19:02:19

回答

1

他们是这个代码没有问题....但也给你一个替代的解决方案,取代的for循环代码

[[[searchBar subviews] objectAtIndex:0] removeFromSuperview]; 
0

我知道已经晚了,但参考这里是

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ { 的CGRect RECT = CGRectMake(0.0F,0.0F,1.0F,1.0F); UIGraphicsBeginImageContext(rect.size); 的UIImage *图像=零; CGContextRef context = UIGraphicsGetCurrentContext(); (上下文)CGContextSetFillColorWithColor(context,[UIColor clearColor] .CGColor); CGContextFillRect(context,rect); image = UIGraphicsGetImageFromCurrentImageContext(); } UIGraphicsEndImageContext();在iOS系统7中的另外的UIView添加到的UISearchBar

dispatch_async(dispatch_get_main_queue(), ^{ 
     self.backgroundImage = image; 
    }); 
}); 
3

,这里是为iOS 7的解决方案:

UIView *vw = [yourSearchBar.subviews objectAtIndex:0]; 
for (id img in vw.subviews) { 
    if ([img isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) { 
     [img removeFromSuperview]; 
    } 
} 

你也可以做以下的(也适用于iOS的7):

[yourSearchBar setBackgroundImage:[UIImage new]]; 
[yourSearchBar setTranslucent:YES];