闻道丶iOS(大杂烩)程序员iOS Developer

iOS 隐藏键盘通用方法

2017-03-24  本文已影响2578人  Swift社区

首先介绍一下通用方法

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

上一篇 下一篇

猜你喜欢

热点阅读