横向中自定义UIViewController动画的解决方法? [英] Workaround for custom UIViewController animations in landscape?

查看:114
本文介绍了横向中自定义UIViewController动画的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义动画UIViewController转换,似乎iOS中存在一个错误,它以横向方向搞砸了布局。在主要的动画方法中,我给出了混合的横向和纵向视图。 (在肖像中,视图都是纵向的,所以没问题。)

I have a custom animated UIViewController transition, and it seems that there is a bug in iOS that screws up the layout in landscape orientation. In the main animation method, i'm given a mix of landscape and portrait views. (In portrait the views are all portrait, so no problem.)

- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext;
{
  UIViewController* toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
  UIViewController* fromViewController = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
  UIView *containerView = [transitionContext containerView];

  // fromViewController.view => landscape, transform
  // toViewController.view => portrait, transform
  // containerView => portrait, no transform

  [containerView addSubview:toViewController.view];

  // ...animation... //
}

我知道当视图有变换时, frame 属性是不可靠的 - 所以我猜这是问题的根源。在横向模式下,to / from viewControllers视图具有90度顺时针变换[0 -1 1 0]。我已经尝试使用边界/中心来调整视图的大小和位置,以及删除变换然后重新应用它,但UIKit与我战斗并坚持将视图显示为肖像。烦人!

I know that the frame property is not reliable when a view has a transform - so I'm guessing this is the root of the problem. In landscape mode, the to/from viewControllers views have a 90 deg clockwise transform [0 -1 1 0]. I've tried using bounds/center to size and position the view, as well removing the transform and then reapplying it, but UIKit fights me and insists on displaying the view as portrait. Annoying!

在屏幕截图中,深灰色是UIWindow背景,红色是添加的模态视图控制器,应该覆盖整个屏幕。

In the screenshot, the dark grey is the UIWindow background, and the red is the added modal view controller which should cover the whole screen.

有人找到了解决方法吗?

Anyone found a workaround?

推荐答案

好的,这个修复非常简单:

Ok, the fix is surprisingly simple:

将toViewController框架设置为容器,然后将视图添加到容器中。

Set the toViewController frame to the container before adding the view to the container.

toViewController.view.frame = containerView.frame;
[containerView addSubview:toViewController.view];

更新:还有一个限制,你不知道框架的方向。它最初是肖像,但在屏幕上显示时会延伸到横向。如果你想在右边的视图中滑动,在横向上它可能会从顶部滑入(或者如果查看其他景观则从底部滑入!)

Update: There is still a limitation in that you don't know the orientation of the frame. It is portrait initially, but stretched into landscape when it is displayed on screen. If you wanted to slide in the view from the right, in landscape it might slide in from the "top" (or the bottom if viewing the other landscape!)

这篇关于横向中自定义UIViewController动画的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆