For a login screen with just three textfields and submit button, I want the view to move up when the keyboard appears just enough so that while the field is not hidden, it also does not move up out of view.
The amount of movement needed is such that the submit button is a fixed distance above the keyboard. While it is possible by moving the fields high up on the page to leave room for the keyboard, the submit button is still hidden
I tried just adding the following:
-(void) viewWillAppear:(BOOL)Animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
This moves the view up a fixed amount but so much so that the fields are not visible for editing, i.e. they are too high up.
Another SO answer suggested:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField:textField up:NO];
}
-(void)animateTextField:(UITextField*)textField up:(BOOL)up
{
const int movementDistance = -200; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
I can't figure out how to implement this. If you just leave it as is nothing happens. I guess you are supposed to rename textfield with the name of your textfield but in that case, would you do it for each of the textfields? I cannot get it to have any effect.
Another suggestion is to use a category such as TPKeyboardAvoiding however this requires a scrollview that I do not need in this case.
Is there no straightforward solution in 2015 for this issue?
Handling the Keyboard Appearing Notification There are two things to do in keyboardWasShown to scroll the text view. First, set the text view's content inset so the bottom edge is the keyboard's height. Second, set the scroll indicator insets to the text view's content inset.
addDoneKeyboardButton() — creates the keyboard done button using UIToolbar. Inside the toolbar, we create a UIBarButtonItem.
Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.
The following animation will move your view (viewForLogin in this case) 200 pixels above when the user starts typing. The view will animate back to original position when the textfield ends editing. Do not forget to set the delegates for the textfields.
Swift 3
func textFieldDidBeginEditing(_ textField: UITextField) {
UIView.animate(withDuration: 0.3, animations: {
self.view.frame = CGRect(x:self.view.frame.origin.x, y:self.view.frame.origin.y - 200, width:self.view.frame.size.width, height:self.view.frame.size.height);
})
}
func textFieldDidEndEditing(_ textField: UITextField) {
UIView.animate(withDuration: 0.3, animations: {
self.viewSupport.frame = CGRect(x:self.viewSupport.frame.origin.x, y:self.viewSupport.frame.origin.y + 200, width:self.viewSupport.frame.size.width, height:self.viewSupport.frame.size.height);
})
}
Objective-C
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y -200., self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
-(void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:TRUE];
self.view.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y +200., self.view.frame.size.width, self.view.frame.size.height);
[UIView commitAnimations];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With