iOS让键盘消失,取消第一响应

1.通过手势
1
2
3
4
5
6
7
8
9
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide)];
tapGestureRecognizer.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGestureRecognizer];

-(void)keyboardHide
{
[self.view endEditing:YES];
}

2.view的touchesBegan
1
2
3
4
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  [self.view endEditing:YES];
}

3.UIScrollView
1
2
3
4
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[self.view endEditing:YES];
}

或者

1
2
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;