Custom transition for push animations using NavigationController on iOS 9

I have a custom transition between view controllers built into the UINavigationController , which works great when building with iOS 7/8, but is not the correct layout when it is built against the iOS 9 SDK.

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

Then it continues and performs the animation. The problem is that the contents of the toViewController , even if it uses the layout restriction of the layout of the top right top sheet, represents the content located behind the navigation bar.

However, it works fine on iOS 8, and if we force redraw (for example, send the application to the background and return it, present the modal on top and turn it off, etc.), this will cause the entire system to automatically the layout will redraw itself and toViewController see the transitions to the desired location (as the top placement guide, x pixels from the navigation bar, not x pixels from the top of the device’s screen).

Adding

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

It works if it is placed in viewDidAppear:animated , but does not work on viewDidLoad or viewWillAppear:animated . This is not a solution, as users will see a jump in the view when redrawing occurs in viewDidAppear:animated

+8
ios ios9 uinavigationcontroller custom-transition
Sep 24 '15 at 10:43
source share
1 answer

I managed to fix my problem by adding the following line to addSubview: ::

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

From the Apple documentation for finalFrameForViewController:

Returns the destination frame rectangle for the specified view view controllers.

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

+22
Sep 24 '15 at 11:38
source share



All Articles