繁体   English   中英

使用自定义过渡时,如何处理呈现的UIViewController中的方向变化

[英]How do I handle orientation changes in the presenting UIViewController when using a custom transition

我有一个UIViewController作为根视图控制器,它使用自定义过渡呈现了另一个UIViewController。 当您在查看子视图控制器时旋转设备,然后返回到根视图控制器时,根视图控制器的视图框架不会更新。 我将其范围缩小到存在自定义过渡,即,如果您没有自定义过渡,则可以正常工作。 进行自定义过渡并让根视图控制器适当处理方向更改的最佳方法是什么?

我在这里有一个演示项目: https : //github.com/rawbee/TestOrientation

似乎您只是忘记为要显示的视图控制器设置框架:

toVC.view.frame = fromVC.view.frame

只需将这一行添加到animateTransition使该方法如下所示:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    guard let fromVC = transitionContext.viewController(forKey: .from),
        let toVC = transitionContext.viewController(forKey: .to),
        let snapshot = fromVC.view.snapshotView(afterScreenUpdates: false)
        else { return }

    let containerView = transitionContext.containerView

    // you want the newly presented view to have the same frame as the old one
    toVC.view.frame = fromVC.view.frame

    containerView.addSubview(toVC.view)
    containerView.addSubview(snapshot)

    let animator = UIViewPropertyAnimator(duration: transitionDuration(using: transitionContext), curve: .easeInOut) {
        snapshot.alpha = 0.0
    }
    animator.addCompletion { _ in
        snapshot.removeFromSuperview()
        transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
    }
    animator.startAnimation()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM