iOS 隐藏键盘通用方法

首先介绍一下通用方法

遵循UITextFieldDelegate的代理方法

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

[self.view endEditing:YES];

}

这个方法只能适用控件是放在view上面,才有效,当遇到UIScrollView、UITableView就不太好使了。

下面介绍一种万能用法,使用手势隐藏键盘

UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollTap:)];

[_myTableView addGestureRecognizer:myTap];

在手势方法中隐藏键盘

- (void)scrollTap:(id)sender {

[self.view endEditing:YES];

}


###iOS技术交流群:668562416

#### 源码Demo获取方法

关注「网罗开发」公众号 ,有iOS demo、RN 视频以及demo、Android demo等你领取。

***

![](http://upload-images.jianshu.io/upload_images/2829694-48307b4d71bc5800.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/300)

小专栏:https://xiaozhuanlan.com/u/fanbaoying

你可能感兴趣的:(iOS 隐藏键盘通用方法)