Reputation: 11782
I am trying to dismiss keyboard on tap anywhere in the view. Here is my code
- (void)registerForNotifcationOfKeyboard
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect bkgndRect = activeField.superview.frame;
bkgndRect.size.height += kbSize.height;
[activeField.superview setFrame:bkgndRect];
[scrollView setContentOffset:CGPointMake(0.0,kbSize.height/2 - activeField.frame.origin.y) animated:YES];
}
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
}
- (void) textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
-(BOOL) disablesAutomaticKeyboardDismissal
{
return NO;
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)viewDidLoad
{
[self registerForNotifcationOfKeyboard];
self.progressBar.hidden = YES;
UIGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
[super viewDidLoad];
}
-(void) dismissKeyboard
{
[activeField resignFirstResponder];
}
when I press tap anywhere this function dismissKeyboard
does get called but it never dismiss the keyboard.
Kindly If anyone has any idea
Best Regards
Upvotes: 0
Views: 233
Reputation: 19926
This always works for me:
//if `dismissKeyboard` is located in your `UIViewController`'s sublass:
-(void) dismissKeyboard
{
[self.view endEditing:YES];
}
From UIView Class Reference:
This method looks at the current view and its subview hierarchy for the text field that is currently the first responder.
If it finds one, it asks that text field to resign as first responder. If the force parameter is set to **YES**, the text field is never even asked; it is **forced to resign**.
Upvotes: 3
Reputation: 1102
if you want to dismiss your keyboard by pressing anywhere inside your view then there are 2 things you can do:
1:- Have a UIButton which has [UIColor clearColor] and which is equal to the size of the view and in the IBAction of the button you dismiss your keyboard. This is not a good practice though it seems to work.
2:- Go to identity inspector and change your view's class to UIControl class and then add an IBAction to your class that dismisses your keyboard.
I hope this helps. Cheers!!
Upvotes: 0
Reputation: 4789
Try the below code.
before that use UITextFieldDelegate in .h file.
in .m file
textfieldname.delegate=self;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textfieldname resignFirstResponder];
return YES;
}
The above code will dismiss keyboard on pressing return key.use below code inside your function to dismiss the keyboard.
[textfieldname resignFirstResponder];
Upvotes: 0
Reputation: 38259
I complelety agree with @Lukasz : Use view's endEditing property to dissmiss keyboard
-(void) dismissKeyboard
{
[self.view endEditing:YES]; //this will dissmiss keyboard;
}
Upvotes: 1