UIViewControllerAnimatedTransitioning with Safe Area Insets on iPhone X

前提是你 提交于 2019-12-23 09:11:17

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!