目前最简单和直接的隐藏键盘的方法,经过覆盖hitTest:withEvent:方法并调用endEditing:方法来隐藏键盘。spa
先上代码code
@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类便可。继承