iOS 隐藏键盘通用方法

作者: Swift社区 | 来源:发表于2017-03-24 11:48 被阅读2578次

首先介绍一下通用方法

遵循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

相关文章

网友评论

本文标题:iOS 隐藏键盘通用方法

本文链接:https://www.haomeiwen.com/subject/ailtottx.html