簡體   English   中英

推送后,如何防止源視圖控制器消失?

[英]How do I keep the source view controller from disappearing after a push segue?

首先,這是我的視圖控制器/ segue設置: 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就會消失。 這是一個屏幕錄像:

並重新出現源視圖控制器的屏幕記錄

(請忽略小故障,我用來上傳此視頻的應用顯然破壞了視頻)

從技術上講,Show segue沒有“ Over Current Context”的問題,因此,我不應該指望源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
    }
}

但是,這會使您失去交互式的關閉手勢(從屏幕左側滑動以關閉),因此您需要自己進行修復。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM