搜索框制作:
1.遵守协议:
UISearchBarDelegate
2.创建搜索:
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,44)];
searchBar.placeholder = @"60万款应用搜搜看";
searchBar.delegate = self;
self.tableView.tableHeaderView = searchBar;
3.实现代理方法:
#pragma mark - UISearchBar代理
//开始输入文字的时候调用
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
//显示取消按钮
searchBar.showsCancelButton = YES;
//将cancel改成中文
UIView *firstSub = [searchBar.subviews firstObject];
for (UIView *sub in firstSub.subviews) {
if ([sub isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
UIButton *btn = (UIButton *)sub;
//修改背景图片
[btn setBackgroundImage:[UIImage imageNamed:@"buttonbar_action"] forState:UIControlStateNormal];
//修改文字
[btn setTitle:@"取消" forState:UIControlStateNormal];
//修改字体大小
btn.titleLabel.font = [UIFont systemFontOfSize:14];
}
}
}
//点击取消按钮时的操作
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
//隐藏键盘
[searchBar resignFirstResponder];
//隐藏取消按钮
searchBar.showsCancelButton = NO;
}
//点击键盘的search按钮调用
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
//隐藏键盘
[searchBar resignFirstResponder];
//隐藏取消按钮
searchBar.showsCancelButton = NO;
//跳转到搜索结果界面
CHSearchController *ctrl = [[CHSearchController alloc] init];
//关键字
// ctrl.keyword = searchBar.text;
// //类型
// ctrl.type = SearchTypeLimit;
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:ctrl animated:YES];
self.hidesBottomBarWhenPushed = NO;
}