Skip to content

Instantly share code, notes, and snippets.

@kukat
Created August 27, 2012 16:07
Show Gist options
  • Save kukat/3489883 to your computer and use it in GitHub Desktop.
Save kukat/3489883 to your computer and use it in GitHub Desktop.
tap anywhere to dismiss keyboard
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(keyboardWillShow:) name:
UIKeyboardWillShowNotification object:nil];
[nc addObserver:self selector:@selector(keyboardWillHide:) name:
UIKeyboardWillHideNotification object:nil];
tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(didTapAnywhere:)];
-(void) keyboardWillShow:(NSNotification *) note {
[self.view addGestureRecognizer:tapRecognizer];
}
-(void) keyboardWillHide:(NSNotification *) note
{
[self.view removeGestureRecognizer:tapRecognizer];
}
-(void)didTapAnywhere: (UITapGestureRecognizer*) recognizer {
[self.view endEditing:YES];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment