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

iOS 隐藏键盘方法

作者: Json_z | 来源:发表于2017-08-25 14:40 被阅读5次

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

}

可以直接调用resignFirstResponder方法

[[UIApplicationsharedApplication]sendAction:@selector(resignFirstResponder)to:nilfrom:nilforEvent:nil];

相关文章

网友评论

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

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