当编辑表单的时候,可能需要点击空白的地方关闭键盘,但是当tableview占整个屏幕的时候没有空白处进行点击,这时候就需要通过添加手势tap来解决。
具体实现方法:
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapView:)]; //tap 手势一共两个属性,一个是设置轻拍次数,一个是设置点击手指个数 tap1.numberOfTapsRequired = 1; tap1.numberOfTouchesRequired = 1; tap1.delegate = self; [self.view addGestureRecognizer:tap1]; - (void)tapView:(UITapGestureRecognizer *)sender{ [self.searchV endEditing:YES]; } #pragma mark - UIGestureRecognizerDelegate - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ for (UIView * cell in self.tableView.visibleCells) { if ([touch.view isDescendantOfView:cell]) { return NO; } } return YES; }