簡體   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