2012-01-12 107 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];