CYC-UISearchBar 点击空白回收键盘

来源:互联网 发布:知乎ios源码 编辑:程序博客网 时间:2024/06/06 02:44
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 0
原创粉丝点击