CYC-UISearchBar 点击空白回收键盘


ios--系统发出的通知--键盘通知

// 监听键盘将要显示的通知 如果要显示  那么用keyboardWillShow来响应

// 使用时注意 当这个页面消失的时候 立马移除所有监听

- (void)viewDidLoad {

//  添加通知 执行键盘回收
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)name:@"UIKeyboardWillHideNotification"object:nil];
    [super viewDidLoad];
}
#pragma mark - 表头添加searchBar
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    SeacherView *seacherView = [[SeacherView alloc]init];
    seacherView.tag =100;
    seacherView.seachBar.delegate = self;
    seacherView.seachBar.showsBookmarkButton = NO;
    self.tableView.tableHeaderView = seacherView;
    return seacherView;
}



// 添加搜索框:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    [searchBar setShowsCancelButton:YES animated:NO];
    // 让tableView 下拉 交换 为 NO
    self.tableView.allowsSelection = NO;
    self.tableView.scrollEnabled = NO;

    // 添加蒙版
        UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
        aView.backgroundColor = [UIColor clearColor];
        aView.alpha = 0.1;
        [self.view addSubview:aView];
    aView.tag = 10;

}
// 添加Cancel:

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    searchBar.text = @"";
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];

    self.tableView.allowsSelection = YES;
    self.tableView.scrollEnabled = YES;
    UIView *aView = [self.view viewWithTag:10];
    // 移除蒙版
    [aView removeFromSuperview];
}


// 添加搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{


    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];

    self.tableView.scrollEnabled = YES;
    self.tableView.allowsSelection = YES;
    self.tableView.allowsSelection = YES;
    self.tableView.scrollEnabled = YES;
    UIView *aView = [self.view viewWithTag:10];
    [aView removeFromSuperview];

    // 点击搜索 把输入的字传到下个界面
    SeacherViewController *seacherVC = [[SeacherViewController alloc] init];
    NSString *nameStr = searchBar.text;
     seacherVC.name = nameStr;
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:seacherVC animated:YES];

}

#pragma mark - 键盘回收
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    // 计算搜索框范围 范围内不执行方法 之外执行键盘回收
    CGPoint touchPoint = [touch locationInView:self.view];
    if (touchPoint.x > 50 && touchPoint.x < kScreenWidth - 100 && touchPoint.y > 10 && touchPoint.y < 40) {
        NSLog(@"x = %f, y = %f", touchPoint.x, touchPoint.y);
    } else {
        [self.view endEditing:YES];
    }
}

#pragma mark - 通知方法执行 移除蒙版 还有 点击方法
- (void)keyboardWillHide:(NSNotification *)notification
{
    UIView *aView = [self.view viewWithTag:10];
    [aView removeFromSuperview];
    SeacherView *seacherView = (SeacherView *)[self.view viewWithTag:100];
    [seacherView.seachBar setShowsCancelButton:NO animated:YES];
    [seacherView.seachBar resignFirstResponder];
    self.tableView.allowsSelection = YES;

    self.tableView.scrollEnabled = YES;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值