从另一个UIViewController Swift iOS中删除View Controller

时间:2016-11-09 22:45:21

标签: ios swift uiviewcontroller

我想知道如何删除该应用目前不在的视图控制器?

让我说我从OldViewController到NewViewController。一旦NewVC加载,我怎么能删除OldVC,而应用程序仍然在NewVC上?

1 个答案:

答案 0 :(得分:1)

我已经尝试了unwind seguesdismiss nested presentingViewController解决方案,但两者仍然存在中间控制器在动画期间可见的问题。

然后我发现这个suggestion我用不同的快照生成修改了,因为它对我不起作用。

可能不是最佳解决方案,但仍然可能有所帮助。

帮助扩展程序:

extension UIViewController {
    func dismissModalStack(animated: Bool, completion: (()->Void)?) {
        guard let snapshot = UIApplication.shared.delegate?.window??.snapshotView() else { return }
        self.presentedViewController?.view.insertSubview(snapshot, at: Int.max)
        self.dismiss(animated: true, completion: completion)
    }
}

extension UIView {
    public func snapshotImage() -> UIImage? {
        UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, 0)
        drawHierarchy(in: bounds, afterScreenUpdates: false)
        let snapshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return snapshotImage
    }

    public func snapshotView() -> UIView? {
        guard let snapshotImage = snapshotImage() else { return nil }
        return UIImageView(image: snapshotImage)
    }
}

用法:

在上一个呈现的视图控制器中:

@IBAction func dismissPressed() {
    self.presentingViewController?.presentingViewController?.dismissModalStack(animated: true, completion: nil)
}

<强>结果:

enter image description here