Swift 在提交之前继续自定义转换布局

Swift segue custom transitions layout before commit

我有两个视图控制器,A 和 B。我可以通过 segue 从 A 转到 B,并且我通过实现 UISegue class 在 segue 上实现了淡入动画。但是,在设备旋转和展开过程中出现问题(从 B 到 A,而在 B 中发生了旋转)。

出现的问题是要呈现的视图(视图 A)布局错误。在转换不起作用之前调用函数 setNeedsUpdateConstraints()layoutSubviews() 或类似函数。屏幕截图描述了 A 横向打开,然后执行到 B 的情况。在 B 时,设备旋转回纵向并展开到 A。

转场的 Swift 代码:

class SegueFadeOut: UIStoryboardSegue {

    override func perform() {
        UIView.transition(from: source.view, to: destination.view, duration: 5.5, options: [.transitionCrossDissolve]) { (success) in
            self.destination.dismiss(animated: false, completion: nil)
        }
    }

}

class SegueFadeIn: UIStoryboardSegue {

    override func perform() {
        let toViewController = self.destination
        let fromViewController = self.source

        let containerView = fromViewController.view.superview

        toViewController.view.alpha = 0.0
        containerView?.addSubview(toViewController.view)

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
            toViewController.view.alpha = 1.0
        }, completion: { success in
            fromViewController.present(toViewController, animated: false, completion: nil)
        })
    }

}

关于如何或在何处实现解决此布局问题的功能的任何线索?谢谢!

我已设法修复它。我可以在执行动画之前设置帧。我也停止使用 UIView.transition(...) 函数,因为它在关闭 fromViewController 期间给出了一些错误。

class SegueFadeOut: UIStoryboardSegue {

    override func perform() {
        let toViewController = self.destination
        let fromViewController = self.source

        let containerView = fromViewController.view.superview

        toViewController.view.alpha = 0.0
        // ADDED LINE BELOW
        toViewController.view.frame = fromViewController.view.frame

        containerView?.addSubview(toViewController.view)

        UIView.animate(withDuration: 0.5, delay: 0, options: .curveEaseInOut, animations: {
            toViewController.view.alpha = 1.0
        }, completion: { success in
            fromViewController.dismiss(animated: false, completion: nil)
        })
    }

}