【问题标题】:How do I keep the source view controller from disappearing after a push segue?如何防止源视图控制器在推送后消失?
【发布时间】:2018-06-01 23:39:30
【问题描述】:

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

最右边的三个视图控制器的背景视图是 UIVisualEffectViews,通过它应该可以看到源视图控制器。它们被添加到各种viewDidLoad()s 中,如下所示:

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 不会消失,但必须有一种方法可以在没有自定义的情况下完成这项工作继续。

【问题讨论】:

    标签: ios iphone swift uiviewcontroller segue


    【解决方案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
        }
    }
    

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

    【讨论】:

    • 这就是我所担心的。感谢您提供转换代码,但我想我会完全放弃模糊的背景。
    猜你喜欢
    • 2014-03-28
    • 2010-12-11
    • 2018-05-04
    • 1970-01-01
    • 2016-01-03
    • 2016-02-22
    • 1970-01-01
    • 2015-04-18
    • 1970-01-01
    相关资源
    最近更新 更多