Animate the keyboard in sync with the UIView while edge-swiping back in iOS7 Animate the keyboard in sync with the UIView while edge-swiping back in iOS7 ios ios

Animate the keyboard in sync with the UIView while edge-swiping back in iOS7

Unfortunately, there is no built-in method to do that. I really hope there will be something like UIScrollViewKeyboardDismissModeInteractive for UIViewControllers.

For now, to do any animations in-between viewControllers, you should use a transitionCoordinator:

- (BOOL)animateAlongsideTransition:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))animation                        completion:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))completion;- (BOOL)animateAlongsideTransitionInView:(UIView *)view                               animation:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))animation                              completion:(void (^)(id <UIViewControllerTransitionCoordinatorContext>context))completion;

For the keyboard you should do something like this:

[self.transitionCoordinator animateAlongsideTransitionInView:self.keyboardSuperview                                                   animation:^(id<UIViewControllerTransitionCoordinatorContext> context) {    self.keyboardSuperview.x = self.view.width;}                                                  completion:nil];

As for keyboardSuperview - you can get that by creating a fake inputAccessoryView:

self.textField.inputAccessoryView = [[UIView alloc] init];

Then the superview will be self.textField.inputAccessoryView.superview

It should just work automatically, but sometimes it doesn't because of some conditions.

If the current firstResponder is located inside of active UIViewController and it dismiss throughout UINavigationController mechanism, the expected keyboard animation (horizontal) will be performed automatically. Therefore, sometimes this default behaviour is broken by other strange factors and the keyboard starts to disappear with slide-down animation instead of horizontal animation.

I spent some days with debugging internal UIKit stuff (around methods needDeferredTransition, allowCustomTransition and other) to find one special factor that plays key role in my case.

I discovered that the logic inside UIPeripheralHost checks frame of current UIViewConroller's view, frame of UINavigationController's view (container) and screen size and, if it all doesn’t equal each other, UIPeripheralHost decides that this current situation seems like modal window and sets flag allowCustomTransition = NO. That turn-off UINavigationController-specific horizontal animation.

Fixing issue with frames completely solves my problem.

If you are experiencing same problems, you can try to debug internal UIKit stuff around these private methods and find your conditions that turn off horizontal animation:

You can use if you don't need anything special.

In my case I've got some logic connected with UIKeyboardWillShowNotification and UIKeyboardWillHideNotification that were fired on "swipe-to-back" gesture. I've combine this answer and TAPKeyboardPop and this is what I've got:

- (void)beginAppearanceTransition:(BOOL)isAppearing animated:(BOOL)animated {    [super beginAppearanceTransition:isAppearing animated:animated];    if (isAppearing || !animated || !_keyboardIsShown) {        return;    }    if ([self respondsToSelector:@selector(transitionCoordinator)]) {        UIView *keyboardView = self.searchBar.inputAccessoryView.superview;        [self.searchBar becomeFirstResponder];        [self.transitionCoordinator animateAlongsideTransitionInView:keyboardView                                                           animation:^(id<UIViewControllerTransitionCoordinatorContext> context)         {             CGRect endFrame = CGRectOffset(keyboardView.frame, CGRectGetWidth(keyboardView.frame), 0);             keyboardView.frame = endFrame;         } completion:^(id<UIViewControllerTransitionCoordinatorContext> context)         {             if (![context isCancelled]) {                 [self.searchBar resignFirstResponder];             }         }];    }}


I've added >iOS7 support and logic for knowing when keyboard is shown (_keyboardIsShown is set in UIKeyboardWillShowNotification/UIKeyboardWillHideNotification or in UIKeyboardDidHideNotification/UIKeyboardDidShowNotification).