ios - 使用自定义转换时如何处理呈现 UIViewController 中的方向更改

标签 ios iphone ipad uiviewcontroller

我有一个 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/

相关文章:

ios - 1 个委托(delegate)给 3 个 View Controller

iphone - MKOverlay 和 MKAnnotation 有什么区别?

iphone - 在 iOS 5 上以编程方式关闭 UIAlertView 不会调用 didDismiss 委托(delegate)方法

ios - 将 iPhone 应用程序转换为通用应用程序后,我应该只从销售中删除 iPad 应用程序还是有更好的步骤?

ios - Swift Accelerometer 与 ScrollView 不断崩溃

ios - 如何在 iOS 中将 pdf 页面转换为图像

ios - 在未安装的机器上运行 Cocoa Pods 应用程序

php - Swift3:参数除以 "/"的HTTP POST请求

ios - swift中URLQueryItem的作用是什么?

iphone - iOS开发者计划: Individual vs Company