美文网首页
iOS隐藏键盘通用方法

iOS隐藏键盘通用方法

作者: 马小悦 | 来源:发表于2017-03-24 21:57 被阅读38次

1 .遵循UITextFieldDelegate的代理方法:在return的代理方法里面书写[self.view endEditing:YES];

2.触屏事件触发代理方法时,在方法中调用结束编辑方法:

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

[self.view endEditing:YES];

}

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

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

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

[_myTableView addGestureRecognizer:myTap];

在手势方法中隐藏键盘

- (void)scrollTap:(id)sender {

[self.view endEditing:YES];

}

相关文章

网友评论

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

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