Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIViewControllerAnimatedTransitioning with Safe Area Insets on iPhone X

When animateTransition is called on the transitioning delegate the Safe Area Insets have yet to be set on the 'to view controller'.

I've tried forcing a layout by calling: toViewController.view.setNeedsLayout() and toViewController.view.layoutIfNeeded() without success.

Can anyone suggest a means of either forcing the Safe Area Insets to be set early or determining how I should know the insets in time for the transition to work as it ought.

The effect is that I have a UICollectionViewCell that appears to move down once the transition completes. This is due to the frame having a 0,0 origin initially rather than say 0,44 on the iPhone X which it has eventually once the Safe Area Insets are set, by which time the transition will have completed.

like image 607
Aodh Avatar asked Feb 05 '18 14:02

Aodh


1 Answers

Ensure that the destination view controller is actually in the view hierarchy when you call layoutIfNeeded. I had this same problem, oddly only affecting certain orientations, and fixed it by adding the second and third lines below:

UIViewController * toViewController = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];

// Added these two lines:
toViewController.view.frame = [transitionContext finalFrameForViewController:toViewController];
[transitionContext.containerView addSubview:toViewController.view];

[toViewController.view setNeedsLayout];
[toViewController.view layoutIfNeeded];
like image 127
ManicJason Avatar answered Oct 02 '22 20:10

ManicJason