在iOS 9上使用NavigationController进行推送动画的自定义转换 [英] Custom transition for push animation with NavigationController on iOS 9

查看:119
本文介绍了在iOS 9上使用NavigationController进行推送动画的自定义转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在嵌入在 UINavigationController 中的视图控制器之间进行了自定义推送转换,当使用iOS 7/8构建时它正常工作但在针对iOS 9构建时呈现错误的布局SDK。

I have a custom push transition between view controllers embedded in a UINavigationController which is working fine when built with iOS 7/8 but presents a wrong layout when built against iOS 9 SDK.

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

  [transitionContext.containerView addSubview:toViewController.view];

  ...

然后继续执行动画。问题是 toViewController 的内容,即使它使用正确的顶部布局指南自动布局约束,也会显示导航栏后面的内容。

It then goes on and performs an animation. The issue is that the content of the toViewController, even though it is using the right top layout guide auto layout constraint, presents the content behind the navigation bar.

尽管如此,它在iOS 8上工作正常,如果我们强制重绘(例如,将应用程序发送到后台并将其恢复,在顶部呈现模式并将其解除,等等)导致整个自动布局系统重绘, toViewController 的视图跳转到正确的位置(作为顶部布局指南,导航栏中的x像素而不是x像素从设备屏幕的顶部开始。)

Nevertheless, it works fine on iOS 8 and if we force a redraw (e.g., sending the app to the background and bring it back, present a modal on top and dismiss it, etc) will cause the whole auto layout system to redraw itself and toViewController's view jumps to the right place (as being the top layout guide, x pixels from the navigation bar rather than x pixels from the top of the device's screen).

添加

[self.view setNeedsUpdateConstraints];
[self.view layoutIfNeeded];

如果放入 viewDidAppear:animated ,则有效但不适用于 viewDidLoad viewWillAppear:animated 。这不是解决方案,因为当重绘发生在 viewDidAppear:animated

Works if put in viewDidAppear:animated, but does not work on viewDidLoad or viewWillAppear:animated. This is not a solution as users would see the view jumping when the redraw is happening at viewDidAppear:animated

推荐答案

我设法通过在 addSubview之前添加以下行来解决我的问题:

  toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];

来自Apple的文档 finalFrameForViewController:

From Apple's Documentation for finalFrameForViewController:


返回指定视图控制器的
视图的结束帧矩形。

Returns the ending frame rectangle for the specified view controller’s view.

此方法返回的矩形表示大小$ b过渡结束时相应视图的$ b。对于在演示期间涵盖的视图
,此
方法返回的值可能是CGRectZero,但它也可能是有效的帧
矩形。

The rectangle returned by this method represents the size of the corresponding view at the end of the transition. For the view being covered during the presentation, the value returned by this method might be CGRectZero but it might also be a valid frame rectangle.

这篇关于在iOS 9上使用NavigationController进行推送动画的自定义转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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