Last active
December 17, 2015 05:09
-
-
Save infolock/5556030 to your computer and use it in GitHub Desktop.
Different ways one can dismiss a keyboard by tapping the background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface someController : UIViewController | |
@property (nonatomic, weak) UITextField *someTextField; | |
-(void)dismissKeyboard; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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