键盘消失

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

2.

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

3、

1
[textFiled resignFirstResponder] 这个则是比较常用的让某个textFiled的键盘隐藏。

4.创创建自定义的触摸手势来实现对键盘的隐藏:
[html] view plain copy print?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
//设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。
tapGestureRecognizer.cancelsTouchesInView = NO;
//将触摸事件添加到当前view
[self.view addGestureRecognizer:tapGestureRecognizer];
}
-(void)keyboardHide:(UITapGestureRecognizer*)tap{
[textFiled resignFirstResponder];
}
}