一、UISearchBar单独使用时,设置样式:
UIView *view =[mySearchBar.subviews objectAtIndex:0];
// view.backgroundColor =[UIColor clearColor];
for (UIView *backview in view.subviews) {
if ([backview isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
[backview removeFromSuperview];
break;
}
}
二、UISearchBar和UISearchDisplayController结合使用时:
// searchResultsDataSource 就是 UITableViewDataSource
searchDisplayController.searchResultsDataSource = self;
// searchResultsDelegate 就是 UITableViewDelegate
searchDisplayController.searchResultsDelegate = self;
别忘了这个:
searchDisplayController.delegate = self;
searchBar.tintColor = [UIColor whiteColor];
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIButton *cancelButton;
UIView *topView = self.searchDisplayController.searchBar.subviews[0];
for (UIView *subView in topView.subviews) {
if ([subView isKindOfClass:NSClassFromString(@"UINavigationButton"))]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton) {
//Set the new title of the cancel button
[cancelButton setTitle:@"Annuller"forState:UIControlStateNormal];
}
}
在IOS5/6下这样设置:
- (void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIButton *cancelButton = nil;
for (UIView *subView in self.searchDisplayController.searchBar.subviews) {
if ([subView isKindOfClass:UIButton)]) {
cancelButton = (UIButton*)subView;
}
}
if (cancelButton){
//Set the new title of the cancel button
[cancelButton setTitle:@"Annuller"forState:UIControlStateNormal];
}
}
searchDisplayController.searchBar.barTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nav_bg"]];
[searchDisplayController setActive:NO animated:YES];