目前最簡單和直接的隱藏鍵盤的方法,經過覆蓋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類便可。繼承