首先,这是我的视图控制器/segue设置:
ios - 推送后,如何防止源 View  Controller 消失?-LMLPHP
最右边三个视图控制器的背景视图是UIVisualEffectViews,通过它源视图控制器应该可见。它们被添加到不同的viewDidLoad()中,如下所示:

let blurEffect = UIBlurEffect(style: .dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

self.tableView.backgroundView = blurEffectView

现在,主视图控制器(垃圾日)通过设置视图控制器可见,但是只要两个最右边的VCs中的一个完全显示在屏幕上,设置VC就会消失。这是一个屏幕记录:
Screen recording of the source view controller dis- and reappearing
(请忽略这些小问题,我用来上传的应用程序显然损坏了视频)
我知道从技术上讲,Show segue没有“过流上下文”的东西,因此,我不应该期望源VC不会消失,但必须有一种方法使这个工作没有自定义segue。

最佳答案

我建议您在视图控制器之间创建自定义转换。
我刚刚写了一篇文章并测试了这门课:

class AnimationController: NSObject, UIViewControllerAnimatedTransitioning
{
    var pushing = true

    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.3
    }

    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        let duration = transitionDuration(using: transitionContext)

        let toVc = transitionContext.viewController(forKey: .to)!
        let toView = transitionContext.view(forKey: .to)!

        let fromView = transitionContext.view(forKey: .from)!

        let container = transitionContext.containerView

        if pushing {
            container.addSubview(fromView)
            container.addSubview(toView)
        }

        var finalFrame = transitionContext.finalFrame(for: toVc)
        if pushing {
            finalFrame.origin.x = finalFrame.width
            toView.frame = finalFrame
            finalFrame.origin.x = 0
        } else {
            finalFrame.origin.x = finalFrame.width
        }

        UIView.animate(withDuration: duration, delay: 0, options: .curveEaseOut, animations: {
            if self.pushing {
                toView.frame = finalFrame
            } else {
                fromView.frame = finalFrame
            }
        }) { (_) in
            transitionContext.completeTransition(true)
            if self.pushing {
                container.insertSubview(fromView, belowSubview: toView)
            } else {
                fromView.removeFromSuperview()
            }
        }
    }
}

在您的UINavigationController课程中,请执行以下操作:
class NavigationController: UINavigationController {

    let animationController = AnimationController()

    override func viewDidLoad() {
        super.viewDidLoad()

        delegate = self
    }
}

这个分机:
extension NavigationController: UINavigationControllerDelegate
{
    func navigationController(_ navigationController: UINavigationController, animationControllerFor operation: UINavigationControllerOperation, from fromVC: UIViewController, to toVC: UIViewController) -> UIViewControllerAnimatedTransitioning? {

        animationController.pushing = operation == .push

        return animationController
    }
}

但是,这会使您丢失交互式解除手势(从屏幕左侧滑动以解除),因此您需要自己修复。

关于ios - 推送后,如何防止源 View Controller 消失?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47888620/

10-13 04:29