简体   繁体   English

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

[英]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.

相关问题 在横向方向上呈现UIViewController - Presenting a UIViewController when on landscape orientation 呈现 UIViewController 时如何为视图设置动画? - How do I animate a view when presenting a UIViewController? 将 UIViewController 呈现给自定义时如何更改默认过渡 - How to change the default transition while presenting UIViewController to custom 如何实现从UICollectionViewController到UIViewController的自定义UIViewController转换? - How can I achieve this custom UIViewController Transition from UICollectionViewController to UIViewController? 单击本地通知时显示自定义UIViewcontroller - Presenting a Custom UIViewcontroller when a local notification is clicked UIViewController更改了方向,但被告知不要这样做 - UIViewController changes orientation while told not to do so 呈现具有自定义尺寸的UIViewController - Presenting a UIViewController with a custom size 如何检测 UITabBarController 中的选项卡数量何时随方向发生变化? - How do I detect when the number of tabs changes on orientation in UITabBarController? 如何将Storyboard UIViewController与我的自定义UIViewController类相关联? - How do I associate a Storyboard UIViewController with my custom UIViewController class? 呈现自定义模式视图时,如何在背景中“淡出”控件的颜色? - When presenting a custom modal view, how do I “fade out” the colors of the controls in the background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM