http://www.360doc.com/content/14/0119/19/15164765_346468678.shtml
修改searchaBar的按钮文字和颜色
修改文字
for(id cc in [bar.subviews[0] subviews])
{
if([cc isKindOfClass:[UIButton class]])
{
UIButton *btn = (UIButton *)cc;
[btn setTitle:@"取消"
forState:UIControlStateNormal];
}
}
修改颜色
在这里 如果你直接设置btn setTitleColor 会发现 按钮文字的颜色并没有发生改变 那么怎么办呢,我们可以考虑换一个方法解决NSMutableDictionary *textAttrs=[NSMutableDictionary dictionary];
textAttrs[NSForegroundColorAttributeName]=[UIColor whiteColor];
[[UIBarButtonItem appearance] setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
这样就可以改变按钮颜色了
取消searchbar的背景颜色
bar.backgroundImage = [self imageWithColor:[UIColor clearColor] size:bar.bounds.size];
//取消searchbar背景色
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
searchBar支持空搜索
也就是当searchBar textField为空的时候 搜索按钮可点击
- (void)searchBarTextDidBeginEditing:(UISearchBar *) searchBar
{
UITextField *searchBarTextField = nil;
NSArray *views = ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) ? searchBar.subviews : [[searchBar.subviews objectAtIndex:0] subviews];
for (UIView *subview in views)
{
if ([subview isKindOfClass:[UITextField class]])
{
searchBarTextField = (UITextField *)subview;
break;
}
}
searchBarTextField.enablesReturnKeyAutomatically = NO;
}