【问题标题】:how to prevent keyboard from dismissing while using popViewController custom transition如何在使用popViewController自定义过渡时防止键盘关闭
【发布时间】:2017-10-14 09:40:00
【问题描述】:

为了在全视图中提供交互弹出手势,我在控制器中有一个 UIPanGestureRecognizer,我们可以使用它在视图控制器中的任何位置从左向右滑动以弹出控制器,而不是使用默认的 NavigationController 弹出手势。

当我在视图中使用打开键盘的手势时,键盘也会通过交互解除(默认 NavigationController 弹出手势不会发生),这看起来很奇怪。

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

如何在我的自定义弹出转换弹出 viewController 时防止键盘关闭。

  //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)
    }
}

【问题讨论】:

  • 我认为,由于视图层次结构的变化,键盘被关闭了。
  • UINavigationController interactivePopGestureRecognizer 没有发生同样的情况
  • 您能分享一下您的手势处理程序吗?
  • 添加弹出窗口不应该退出键盘。您可以实现弹出委托以重新打开特定字段的键盘。
  • @AvijitNagare 这是带有自定义过渡而不是弹出窗口的 popViewController。

标签: ios swift uinavigationcontroller popviewcontroller popviewcontrolleranimated


【解决方案1】:

所以我注意到 iOS 13 上的键盘转换与 iOS 12 不同,所以我必须像这样手动进行:

    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
            }
        }
    }

您可以检查我所做的实现 - https://github.com/APUtils/Animators/blob/master/Animators/Classes/Right%20Slide%20Animation/RightSlideOutAnimator.swift

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多