第一种: 使用view的touchesBegan:触摸事件来实现对键盘的隐藏,当点击view的区域就会触发这个事件
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[searchBar resignFirstResponder];
}
第二种:创建自定义的触摸手势来实现对键盘的隐藏:
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
//设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。
tapGestureRecognizer.cancelsTouchesInView = NO;
//将触摸事件添加到当前view
[self.view addGestureRecognizer:tapGestureRecognizer];
}
原文地址:http://zouhao510.blog.51cto.com/4538557/1641162