问题
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