目前最简单和直接的隐藏键盘的方法,通过覆盖hitTest:withEvent:方法并调用endEditing:方法来隐藏键盘。
先上代码
@implementation ALXBackgroundView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *view = [super hitTest:point withEvent:event];
[view endEditing:YES]; //隐藏键盘
return view;
}
@end
ALXBackgroundView直接继承自UIView,在您的代码中把UIViewController根view的实现类(UIView)直接替换为ALXBackgroundView类即可。