UINavigationController calls willMoveToWindow and didMoveToWindow twice on a view, in the case of show animation

笑着哭i 提交于 2020-06-17 01:26:28

问题


I want to be able to check when a view, let's call him myView, goes on/off screen.

I was able to do so by checking if willMoveToWindow and didMoveToWindow are called on myView.

My problem started with UINavaigationController with animation:

Lets say we have a two screens: Screen A with a button that will result in a Show segue with a back button as the navigation control.

The problem is: The views of screen A are called twice once clicking on the button:

  • The first time viewWillMoveToWindow is called with THE REAL WINDOW.
  • Then, in the next cycle, the view is called with a nil window.

In between those two calls, I get an illegal state in which I think the view is about to be presented, although he will be removed soon..

I tried to resolve this by using the _transitionAnimationContext on the navigation controller and see who is the fromViewController. If it is my VC, I will ignore the call, since I am about to be removed. The problem with this approach is the use of private APIs, which I am trying to avoid.

Any ideas??


回答1:


So, I will answer my own question :)

After searching and searching, I came across the following UIViewController extension:

@interface UIViewController(UIViewControllerTransitionCoordinator)
@property(nonatomic, readonly, nullable) id <UIViewControllerTransitionCoordinator> transitionCoordinator NS_AVAILABLE_IOS(7_0);

in UIViewControllerTransitionCoordinator.h

This property holds the current transition coordinator, from which you can reach the fromViewController, which in this case will indicate the view willMoveToWindow for transition and not for presentation.

Enjoy!



来源:https://stackoverflow.com/questions/43306924/uinavigationcontroller-calls-willmovetowindow-and-didmovetowindow-twice-on-a-vie

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