In this tutorial, I will show you how to hide a keyboard in IOS with the keyboard without return button. See the code below.
//add this code at the top under implementation
UITapGestureRecognizer *tapRecognizer;
//In viewdidload add this code below
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:)];
//add the following methods. Note _tf_amount is the name of the textfield.
- (void)keyboardWillShow:(NSNotification *) note{
[self.view addGestureRecognizer:tapRecognizer];
}
- (void)keyboardWillHide:(NSNotification *) note{
[self.view removeGestureRecognizer:tapRecognizer];
}
- (void)didTapAnywhere: (UITapGestureRecognizer *) recognizer{
[_tf_amount resignFirstResponder];
}
That's it.
No comments:
Post a Comment