我正在使用此方法,但键盘未隐藏。
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[_myTextView resignFirstResponder];
}
如何只需触摸键盘外即可关闭键盘。如果解决方案同时适用于客观c和swift,那将会很棒。感谢
我在目标C中查询了scrollView中的textView。请解决。我已经搜索过,但没有发现任何离散的解决方案。
答案 0 :(得分:0)
试试这个
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}
或使用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if (![[touch view] isKindOfClass:[UITextView class]]) {
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}
[super touchesBegan:touches withEvent:event];
}
以上代码不起作用试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *resignView = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(handleSingleTapforResign:)];
resignView.cancelsTouchesInView = NO;
[self.view addGestureRecognizer: resignView];
}
取消目前正在编辑的内容:
- (void) handleSingleTapforResign:(UITapGestureRecognizer *) sender
{
[self.view endEditing:YES];
[self.yourScrollviewName endEditing:YES];
}