Skip to content

Instantly share code, notes, and snippets.

@infolock
Last active December 17, 2015 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save infolock/5556030 to your computer and use it in GitHub Desktop.
Save infolock/5556030 to your computer and use it in GitHub Desktop.
Different ways one can dismiss a keyboard by tapping the background
@interface someController : UIViewController
@property (nonatomic, weak) UITextField *someTextField;
-(void)dismissKeyboard;
@end
import "dismissKeyboard.h"
@implementation dismissKeyboard
/**
* Option 1 - set a gesture recognizer to the view you are currently on to call hideKeyboard
*/
-(void)viewDidLoad {
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
[self.view addGestureRecognizer:gestureRecognizer];
}
-(void)hideKeyboard {
[self.someTextField resignFirstResponder];
}
/**
* Option 2 - Implement UIGestureRecognizer for touchesBegan (registering this method
* via viewDidLoad like above is not needed)
*
* Note, a UIViewController where you do
* not need separate handlers is a good place for this..
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.someTextField resignFirstResponder];
[super touchesBegan:touches withEvent:event];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment