核心代码
[self.view endEditing:YES];
使用
vc
的touchesBegan
事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
- 使用
UITapGestureRecognizer
信号订阅
@weakify(self);
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
@strongify(self);
[self.view.window endEditing:YES];
}];
[self.view addGestureRecognizer:tap];