UISearchBar 修改背景颜色透明
UISearchBar *search=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 100)];
[search setPlaceholder:@"搜索"];
[search setPrompt:@"我的搜索"];
// [search setBarStyle:UIBarStyleBlackTranslucent];
// [search setTintColor:[UIColor redColor]];
// [search setSearchFieldBackgroundImage:[UIImage imageNamed:@"2"] forState:UIControlStateNormal];
// //3自定义背景
//
// UITextField *searchField = [search valueForKey:@"_searchField"];
// searchField.textColor = [UIColor redColor];
// [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//
//
// //修改放大镜图片
// UIImage *image = [UIImage imageNamed:@"cl_tab2_gray"];
// UIImageView *iconView = [[UIImageView alloc] initWithImage:image];
// iconView.frame = CGRectMake(0, 0, image.size.width , image.size.height);
// searchField.leftView = iconView;
//让背景透明
for (UIView *subview in search.subviews) {
for(UIView* grandSonView in subview.subviews){
if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
grandSonView.alpha = 0.5f;
}else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
NSLog(@"Keep textfiedld bkg color");
}else{
grandSonView.alpha = 0.0f;
}
}//for cacheViews
}//subviews