My UIView is not moving up when keyboard shown and disappear when keyboard is hidden as well.
This coding is in ViewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
and this coding is for the rest.
- (void)keyboardDidShow:(NSNotification *)note
{
[self animateTextField:TRUE];
}
- (void)keyboardDidHide:(NSNotification *)note
{
[self animateTextField:FALSE];
}
-(void)animateTextField :(BOOL)up
{
const int movementDistance = -130; // tweak as needed
const float movementDuration = 1.0; // tweak as needed
int movement = (up ? movementDistance : - movementDistance);
[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
_buttonsView.frame = CGRectOffset(_buttonsView.frame, 0, movement);
[UIView commitAnimations];
}
I think this suits your implementation:
-(void)animateTextField :(BOOL)up
{
const int movementDistance = -130; // tweak as needed
const float movementDuration = 1.0; // tweak as needed
int movement = (moveUp ? movementDistance : - movementDistance);
[UIView beginAnimations: @"animateTextField" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
_buttonsView.frame = CGRectOffset(_buttonsView.frame, 0, movement);
[UIView commitAnimations];
[_buttonsView setNeedsLayout];
}
Edit
I tried your code and calling setNeedsLayout
did the work..
With auto-layout you animate the constraint constant, though you don't change the CGRect frames. That was the old way.
Please see this link that I answered a similar question that they were having issues animating the movement of a UIView
Animate UIView with layoutConstraints
This should help, though feel free to let me know if you need anything clarified.
use just below code you don't need to call it from any where
#pragma mark - move view up when keyboard is displayed
- (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 = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
contentView.frame = CGRectOffset( contentView.frame, 0, movement);
[UIView commitAnimations];
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.