在顶级viewControl中设置
- (void)viewDidLoad {
[super viewDidLoad];
//设置全局的触摸空白位置,隐藏键盘功能
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
//设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。
tapGestureRecognizer.cancelsTouchesInView = NO;
//将触摸事件添加到当前view
[self.view addGestureRecognizer:tapGestureRecognizer];
}
-(void)keyboardHide:(UITapGestureRecognizer*)tap{
[self dumpViewHideKeyboard:self.view];
}
- (void)dumpViewHideKeyboard:(UIView *)aView
{
for (id obj in aView.subviews) {
NSLog(@"obj class: %@",[obj class]);
if ([obj isKindOfClass:[UITextField class]]) {
UITextField* theTextField = (UITextField*)obj;
[theTextField resignFirstResponder];
}
else if ([obj isKindOfClass:[UITextView class]]){
UITextView* theTextView = (UITextView*)obj;
[theTextView resignFirstResponder];
}
else if ([obj isKindOfClass:[UIView class]]){
[self dumpViewHideKeyboard:obj];
}
}
}