ios - 使用popViewController自定义过渡时如何防止键盘关闭

标签 ios swift uinavigationcontroller popviewcontroller popviewcontrolleranimated

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

当我在 View 中使用键盘打开的手势时,键盘也会通过交互解除(默认 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)
    }
}

最佳答案

所以我注意到与 iOS 12 相比,iOS 13 上的键盘转换是不同的,所以我不得不像这样手动完成:

    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

关于ios - 使用popViewController自定义过渡时如何防止键盘关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43982273/

相关文章:

ios - 如何更改 UIBarButtonItem 的选择区域?

ios - 错误 : UICollectionView received layout attributes for a cell with an index path that does not exist in Swift

IOS权限以编程方式

ios - swift.h 找不到 Swift 类

swift - 如何更改 UIView 的 z 索引或堆栈顺序?

swift - 添加文本后将 NSTextView 滚动到底部

objective-c - 在其他 UITabbar 上操作导航 Controller 堆栈

c# - 您可以从 iOS C 插件 "on the spot"写入 Unity 纹理吗?

ios - 如何刷新父 View Controller 的viewdidload

iphone - 不符合键值编码