[英]How do I handle orientation changes in the presenting UIViewController when using a custom transition
I have a UIViewController as the root view controller that presents another UIViewController using a custom transition. 我有一个UIViewController作为根视图控制器,它使用自定义过渡呈现了另一个UIViewController。 When you rotate the device while looking at the child view controller, then go back to the root view controller, the root view controller's view frame isn't updated.
当您在查看子视图控制器时旋转设备,然后返回到根视图控制器时,根视图控制器的视图框架不会更新。 I've narrowed it down to the presence of a custom transition, ie if you don't have a custom transition, it works just fine.
我将其范围缩小到存在自定义过渡,即,如果您没有自定义过渡,则可以正常工作。 What would be the best way to have the custom transition and have the root view controller handle orientation changes appropriately?
进行自定义过渡并让根视图控制器适当处理方向更改的最佳方法是什么?
I have a demo project available here: https://github.com/rawbee/TestOrientation 我在这里有一个演示项目: https : //github.com/rawbee/TestOrientation
Seems that you just forgotten to set the frame for the view controller that is being presented: 似乎您只是忘记为要显示的视图控制器设置框架:
toVC.view.frame = fromVC.view.frame
Just add this line to animateTransition
so that the method looks like this: 只需将这一行添加到
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.