问题
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.
回答1:
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];
来源:https://stackoverflow.com/questions/48624676/uiviewcontrolleranimatedtransitioning-with-safe-area-insets-on-iphone-x