7

I have a view controller that can be popped with the new interactivePopGestureRecognizer. If there is a keyboard present and the swipe animation begins the keyboard does not move with the view. I have had a look at this question and implemented it like this in my view controller that gets dismissed

-(void)viewWillDisappear:(BOOL)animated
{ 
  [super viewWillDisappear:animated];

  [self.transitionCoordinator animateAlongsideTransitionInView:self.aTextInputView.keyboardSuperView animation:^(id<UIViewControllerTransitionCoordinatorContext> context) {

    CGRect frame = self.aTextInputView.keyboardSuperView.frame;
    frame.origin.x = self.view.frame.size.width;

    self.aTextInputView.keyboardSuperView.frame = frame;

  } completion:nil];
}

Now what I get when the view animates to disappear is the keyboard animates off the screen to the x point of 320 which makes sense as thats what I set it to, my question is how do I get the keyboard to animate with the swipe back?

Update

For any one that sees a weird animation when the view disappears you can get remove the keyboard by doing this.

[self.transitionCoordinator notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context){
    if (![context isCancelled]) {
        [keyboardSuperview removeFromSuperview];
    }
}];
Community
  • 1
  • 1
sbarow
  • 2,759
  • 1
  • 22
  • 37
  • 1
    +1 for the update! That jerky animation was really bugging me and your snippet works like a charm to fix it! – Matt Apr 08 '14 at 01:37
  • Thank you for the update! The weird keyboard animation has been bugging me for awhile! – PPierson Aug 28 '14 at 16:59

2 Answers2

3

You have a lot of custom code in your snippet, so correct me if I am wrong, but it seems you have incorrect self.aTextInputView.keyboardSuperView.

Double check that it is not nil. If it is, you forgot to add an inputAccessoryView.

Here is the full code snippet without any extensions:

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    UIView *keyboardSuperview = self.textField.inputAccessoryView.superview;
    [self.transitionCoordinator animateAlongsideTransitionInView:keyboardSuperview
                                                       animation:
     ^(id<UIViewControllerTransitionCoordinatorContext> context) {
         CGRect keyboardFrame = keyboardSuperview.frame;
         keyboardFrame.origin.x = self.view.bounds.size.width;
         keyboardSuperview.frame = keyboardFrame;
     }
                                                      completion:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.textField.inputAccessoryView = [[UIView alloc] init];
}
monder
  • 379
  • 3
  • 7
  • If I `NSLog(@"%@", self.aTextInputView.keyboardSuperView);` I get `>` which I assume is what I want? – sbarow Feb 21 '14 at 12:38
  • Yes... I've tried creating a blank MasterDetail application using Xcode wizard, added a `UITextField` on `DetailViewController` storyboard, pasted the code above to the same controller, and it works. – monder Feb 21 '14 at 12:45
  • Hmmm, strange added the input view directly in the viewController and it seems to work now. When it was added in the custom view class it was causing that error. Need to experiment more with this. Thanks for your help. – sbarow Feb 21 '14 at 13:05
  • 2
    Once the view disappears the keyboard quickly goes back to x=0 then disappears, any way to get rid of that? – sbarow Feb 21 '14 at 13:20
  • This is not working for me on iOS9... Does anybody know how to make this animation work? – Andres Nov 04 '15 at 20:34
1

Just found really simple solution for iOS8

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    [self.aTextInputView resignFirstResponder];
}
Nikita Ivaniushchenko
  • 1,425
  • 11
  • 11