可能重复:
Dismiss keyboard on touch anywhere outside UITextField
我在我的应用中使用UIScrollView。我正在放几个文本字段和按钮。我想在文本框外触摸时隐藏键盘。 (我的意思是他们会录制uiscrollview)。我尝试过很多东西......我使用的是UIGestureRecognizer,但它不起作用......请帮忙
答案 0 :(得分:2)
这是刚刚尝试的解决方案,它的工作原理......希望帮助
将以下代码添加到viewDidLoad;
-(void)viewDidLoad {
//create a tapGesture which calls a removeKeyboard method
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.yourScrollView addGestureRecognizer:tapGesture];
}
-(void)removeKeyboard {
[self.yourTextField resignFirstResponder];
}
希望有所帮助......
答案 1 :(得分:1)
在您的视图中尝试以下代码...
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([yourTextView isFirstResponder] && [touch view] != yourTextView) {
[yourTextView resignFirstResponder];
}
}