2

I have a standard UINavigationViewController with a standard UIInteractivePopGestureRecognizer for iOS 7. I also have these well known and widely-used methods:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

They work like this:

- (void)keyboardWillShow:(NSNotification *)notification{
    CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];

    UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;

    if (keyboardIsUp || isAnimating) return;

    isAnimating = YES;

    [UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
        _topConstraint.constant -= kTopConstraintR4Ratio;
        [self.view layoutIfNeeded];
    }completion:^(BOOL finished){
        keyboardIsUp = YES;
        isAnimating = NO;
    }];
}

- (void)keyboardWillHide:(NSNotification *)notification{

    CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];

    UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;

    if (isAnimating) return;

    isAnimating = YES;

    [UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
        _topConstraint.constant = topConstraintOriginalValue;
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        keyboardIsUp = NO;
        isAnimating = NO;
    }];
}

The thing is that UIView animateWithDuration is triggered without any respect to duration when I swipe the view back with a gesture. The whole view turns into a mess - it animates slowly when I pop the view, but when the gesture isn't finished - the view jumps up.

It looks like this:

enter image description here

How do I get rid of this strange behavior? I don't want the view to move during the transition and I want it to stay still.

EDIT:

You can download the sample project using the link here:

https://www.dropbox.com/s/034lfy49ru4pelf/SampleProject.zip

Sergey Grischyov
  • 11,895
  • 19
  • 78
  • 118

2 Answers2

3

Easy hack:

BOOL needAdjustContstrain;

Set needAdjustContstrain to YES in keyboardWillShow:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    if (needAdjustContstrain) {
         _topContstraint.constant -=40;
    }
}
avdyushin
  • 1,892
  • 16
  • 21
-2

you need to call the layoutIfneeded method before the animation block . Check out the code below :

- (void)keyboardWillHide:(NSNotification *)notification{

CGFloat keyboardSlideDuration = [[[notification userInfo] objectForKey: UIKeyboardAnimationDurationUserInfoKey] floatValue];

UIViewAnimationOptions keyboardAnimationCurve = [[notification userInfo][UIKeyboardAnimationCurveUserInfoKey] integerValue]<<16;

if (isAnimating) return;

isAnimating = YES;

[self.view layoutIfNeeded];
[UIView animateWithDuration:keyboardSlideDuration delay:0 options:(keyboardAnimationCurve | UIViewAnimationOptionBeginFromCurrentState) animations:^{
    _topContstraint.constant = 89;
  //
} completion:^(BOOL finished) {
    keyboardIsUp = NO;
    isAnimating = NO;
}];}

Please check the link

Community
  • 1
  • 1
Singh
  • 2,091
  • 3
  • 15
  • 30