5

To provide the interaction pop gesture in full view, i have a UIPanGestureRecognizer in my controller with which we can swipe from left to right any where in the view controller to pop the controller, instead of using the default NavigationController pop gesture.

When i use the gesture with keyboard open in the view the keyboard also dismissing(not occurring with default NavigationController pop gesture) with the interaction, which looks weird.

   func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {
     if operation == .pop {
        return PopControllerTransition()//My transition
    }
    return nil
}

How can i prevent the keyboard dismiss while pop viewController with my custom pop transition.

  //transition code
  func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let containerView = transitionContext.containerView
    let fromController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.from)!
    let toController = transitionContext.viewController(forKey: UITransitionContextViewControllerKey.to)!

    toController.view.transform = CGAffineTransform(translationX: -(toController.view.bounds.width/3), y: 0)
    containerView.insertSubview(toController.view, belowSubview: fromController.view)

    let view = GradientView(frame: containerView.frame)
    view.horizontal = true
    view.backgroundColor = UIColor.clear
    view.transform = CGAffineTransform(translationX: -toController.view.bounds.width, y: 0)
    view.gradientLayer.colors = [UIColor(white: 0.0, alpha: 0.2).cgColor, UIColor(white: 0.0, alpha: 0.5).cgColor]
    containerView.insertSubview(view, belowSubview: fromController.view)

    let duration = transitionDuration(using: transitionContext)

    UIView.animate(withDuration: duration, animations: {
        toController.view.transform = .identity
        fromController.view.transform = CGAffineTransform(translationX: fromController.view.bounds.width, y: 0)
        view.transform = .identity
        view.alpha = 0.0
    }) { (finish) in
        if !transitionContext.transitionWasCancelled {
            fromController.view.removeFromSuperview()
        }
        view.removeFromSuperview()
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    }
}
Madhu
  • 984
  • 1
  • 11
  • 32

1 Answers1

0

So I noticed that keyboard transition is different on iOS 13 compared to iOS 12 so I had to do it manually like that:

    let animations: () -> Void = {
        fromView.frame.origin.x += fromView.bounds.width
        toView.frame.origin.x += fromView.bounds.width

        // Keyboard behavior changed on iOS 13. It dismisses ignoring transition
        // so need to fix that manually.
        if #available(iOS 13.0, *) {
            let keyboardWindow = UIApplication.shared.windows
                .first { String(describing: type(of: $0)) == "UIRemoteKeyboardWindow" }

            if let keyboardWindow = keyboardWindow, let keyboardImage = keyboardWindow.getImage() {
                let imageView = UIImageView(image: keyboardImage)
                keyboardWindow.addSubview(imageView)
                keyboardWindow.frame.origin.x += keyboardWindow.frame.width
            }
        }
    }

You can check implementation I did - https://github.com/APUtils/Animators/blob/master/Animators/Classes/Right%20Slide%20Animation/RightSlideOutAnimator.swift

Anton Plebanovich
  • 978
  • 14
  • 12