#pragma - mark- 开始的时候
/* 开始 */
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
/* 右边的那个按钮 */
searchBar.showsCancelButton = YES;
[self.searchBarView setHidden:NO];
/* 开始进入编辑状态的时候让返回键隐藏 */
[self.backButton setHidden:YES];
#pragma mark - 改变搜索按钮的title 不明白***************
for(id cc in [searchBar.subviews[0] subviews])
{
if([cc isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)cc;
[button setFrame:CGRectMake(0, 0, 100 * HEIGHT, 40 * HEIGHT)];
[button setTitle:@"取消" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
}
}
return YES;
}
#pragma mark - 实现取消的点击事件
- (void)buttonClick:(UIButton *)button {
[self.searchBarView setHidden:YES];
/* 点击取消键让返回键显示 */
[self.backButton setHidden:NO];
}
#pragma mark - 结束
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
/* 让右边取消的按钮隐藏 */
searchBar.showsCancelButton = NO;
}
#pragma mark - 点击搜索按钮的时候
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
self.keyWord = self.searchBar.text;
ZYHPostContentViewController *post = [[ZYHPostContentViewController alloc] init];
post.titleContent = self.searchBar.text;
[self.navigationController pushViewController:post animated:YES];
}
#pragma mark - 点击取消按钮的时候
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
/* 清除让search里面的字 */
self.searchBar.text = @"";
/* 键盘回收 放弃第一响应 */
[searchBar resignFirstResponder];
[self.backButton setHidden:YES];
}
#pragma mark - 点击空白处的时候
- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event {
self.searchBar.text = @"";
[self.searchBarView setHidden:YES];
}