在iOS5中隐藏UISearchBar背景

时间:2012-01-12 18:15:13

标签: iphone ios4

隐藏UIsearchBar背景的下面代码在iOs4.2之前正常工作,但在iOS4.3或更高版本中没有。

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

3 个答案:

答案 0 :(得分:3)

在iOS 7中,另一个UIView被添加到UISearchBar,这是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];

答案 1 :(得分:1)

对于这段代码来说,它们没有问题....但是也为你提供了另一种解决方案,将for循环的代码替换为

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

答案 2 :(得分: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 * image = nil;         CGContextRef context = UIGraphicsGetCurrentContext();         if(context){             CGContextSetFillColorWithColor(context,[UIColor clearColor] .CGColor);             CGContextFillRect(context,rect);             image = UIGraphicsGetImageFromCurrentImageContext();         }         UIGraphicsEndImageContext();

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