如何在推送segue后让源视图控制器不会消失?

时间:2017-12-19 13:59:42

标签: ios iphone swift uiviewcontroller segue

首先,这是我的视图控制器/ segue设置: Screenshot of Interface Builder

三个最右边的视图控制器的后台视图是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

现在,通过设置视图控制器可以看到主视图控制器(“垃圾日”),但只要两个最右边的VC中的一个完全在屏幕上,VC的设置就会消失。这是一个屏幕录制:

Screen recording of the source view controller dis- and reappearing

(请忽略这些故障,我上传的应用程序显然已损坏视频)

我从技术上讲,Show segue没有“Over Current Context”,因此,我不应该期望源VC不会消失,但必须有一种方法可以使这个工作没有自定义塞格斯。

1 个答案:

答案 0 :(得分:1)

我建议您在视图控制器之间创建自定义转换。

我刚刚编写并测试了这个类:

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

然而,这会让你失去交互式解雇手势(从屏幕左侧轻扫以解散)所以你需要自己解决这个问题。