8

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];

  ...

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.

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).

Adding

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

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

4

1 回答 1

24

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

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

来自 Apple 的finalFrameForViewController 文档:

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

该方法返回的矩形表示过渡结束时对应视图的大小。对于演示期间覆盖的视图,此方法返回的值可能是 CGRectZero 但它也可能是有效的框架矩形。

于 2015-09-24T11:38:11.240 回答