//UISearchBar的初始化
_searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(40, 26, BESCREEN_W-45, 30)];
_searchBar.delegate = self;
_searchBar.placeholder = @"搜索学校名称";
//修改searchBar搜索框的背景颜色
UIImageView *barImageView = [[[_searchBar.subviews firstObject] subviews] firstObject];
barImageView.image = BEIMAGENAME(@"状态栏+导航栏");
//修改searchBar搜索的小图标
[_searchBar setImage:[UIImage imageNamed:@"圆角矩形-659"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
[self.bgImageView addSubview:_searchBar];
#pragma mark - 搜索栏代理
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
//修改cancel按钮
[UIView animateWithDuration:0.1 animations:^{
} completion:^(BOOL finished) {
self.searchBar.frame = CGRectMake(8, 26, BESCREEN_W-8, 30);
UIImageView *barImageView = [[[_searchBar.subviews firstObject] subviews] firstObject];
barImageView.image = BEIMAGENAME(@"状态栏+导航栏");
self.searchBar.showsCancelButton = YES;
[self.view addSubview:self.grayView];
for(UIView *view in [[[self.searchBar subviews] objectAtIndex:0] subviews]) {
if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
UIButton * cancel =(UIButton *)view;
[cancel setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancel setTitle:@"取消" forState:UIControlStateNormal];
cancel.titleLabel.font = FontSet(13);
}
}
//修改输入框的字体大小和颜色
for (UIView* subview in [[searchBar.subviews lastObject] subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField*)subview;
textField.textColor = TITLE_COLOR; //修改输入字体的颜色
textField.font = FontSet(13);
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:13] forKeyPath:@"_placeholderLabel.font"];
[textField setValue:BECOLOR(176, 176, 176, 1) forKeyPath:@"_placeholderLabel.textColor"]; //修改placeholder的颜色
}
}
}];
return YES;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
if (searchText.length>0) {
self.isSearch = YES;
[self.grayView removeFromSuperview];
self.grayView = nil;
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", self.searchBar.text];
self.filterArray = [[self.nameArray filteredArrayUsingPredicate:searchPredicate] mutableCopy];
if (self.filterArray.count==0) {
self.tableView.hidden = YES;
[self.view addSubview:self.tipLabel];
}else{
self.tableView.hidden = NO;
[self.tipLabel removeFromSuperview];
self.tipLabel=nil;
}
}else{
self.isSearch = YES;
[self.tipLabel removeFromSuperview];
self.tipLabel=nil;
[self.view addSubview:self.grayView];
}
[self.tableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[UIView animateWithDuration:0.1 animations:^{
} completion:^(BOOL finished) {
_searchBar.frame = CGRectMake(40, 26, BESCREEN_W-45, 30);
UIImageView *barImageView = [[[_searchBar.subviews firstObject] subviews] firstObject];
barImageView.image = BEIMAGENAME(@"状态栏+导航栏");
self.searchBar.showsCancelButton = NO;
}];
self.isSearch = NO;
[self.grayView removeFromSuperview];
[self.tipLabel removeFromSuperview];
self.tableView.hidden = NO;
self.searchBar.text = @"";
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[self.searchBar resignFirstResponder];
}