我有一个 UIViewController 作为 Root View Controller ,它使用自定义转换呈现另一个 UIViewController。当您在查看 subview Controller 的同时旋转设备,然后返回到 Root View Controller 时, Root View Controller 的 View 框架不会更新。我已将其范围缩小到存在自定义过渡,即如果您没有自定义过渡,它也可以正常工作。进行自定义转换并让 Root View Controller 适当处理方向更改的最佳方法是什么?
我这里有一个演示项目:https://github.com/rawbee/TestOrientation
最佳答案
似乎您只是忘记为正在呈现的 View Controller 设置框架:
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()
}
关于ios - 使用自定义转换时如何处理呈现 UIViewController 中的方向更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48652699/