ios中隐藏键盘的方式

来源:互联网 发布:js数组元素重新排序 编辑:程序博客网 时间:2024/05/17 04:52

1. 重写view的 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 方法


2. 为view添加tapGesture

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];tapGesture.cancelsTouchesInView = NO;[view addGestureRecognizer:tapGesture];- (void)hideKeyboard{    [self.view endEditing:NO];}

假如view是tableview或者collectionView,如果不设置

tapGesture.cancelsTouchesInView = NO;
那么原来的cell将变得不能点击。


[self.view endEditing: NO]的解释

Apple Doc:

"This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign."


3. 可以设置一个透明的大的button响应事件。

0 0