Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep UIKeyboard with view when swiping back iOS 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];
    }
}];
like image 298
sbarow Avatar asked Feb 21 '14 08:02

sbarow


2 Answers

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];
}
like image 82
monder Avatar answered Sep 28 '22 21:09

monder


Just found really simple solution for iOS8

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

    [self.aTextInputView resignFirstResponder];
}
like image 41
Nikita Ivaniushchenko Avatar answered Sep 28 '22 21:09

Nikita Ivaniushchenko