ios - 如何防止源 View Controller 在推送后消失?

标签 ios iphone swift uiviewcontroller segue

首先,这是我的 View Controller /segue 设置: Screenshot of Interface Builder

最右边的三个 View Controller 的背景 View 是 UIVisualEffectViews,源 View Controller 应该通过它们可见。它们被添加到各种 viewDidLoad() 中,如下所示:

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

self.tableView.backgroundView = blurEffectView

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

Screen recording of the source view controller dis- and reappearing

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

从技术上讲,Show segue 没有“Over Current Context”之类的东西,因此,我不应该期望源 VC 不会消失,但必须有一种方法可以在没有自定义的情况下使它工作继续。

最佳答案

我建议您在 View Controller 之间创建自定义转换。

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

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/

相关文章:

ios - UIButton 没有出现在 iPhone 5S 上

ios - 为什么我不能在表格 View 中显示我的图像?

iphone - 百分比计算总是返回 0

iphone - 设置UITableView contentOffset,然后拖动, View 偏移跳转到新位置

ios - 在 iOS 15 上实现消息 - 与你共享功能

swift - 如果互联网连接丢失,如何停止函数执行

ios - 在 UITableView 中单击的事件

ios - 如何发现 iOS 中的可用内存量?

iphone - iOS 中的通话和短信计数器

json - 如何使用单个 JSON 更新整个 Realm