使用自定义过渡时,如何处理uiviewController的方向更改



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

我在这里有一个演示项目: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()
}

相关内容

  • 没有找到相关文章

最新更新