How to cancel PresentViewController animation

1k views Asked by At

I have viewController1 (in a UINavigationController)that presents viewController2 The problem is that while the viewController2 is being presented (takes about 0.2s), an event in the app can cause viewController1 to be popped.

Then I get:

unbalanced calls to begin/end appearance transitions for DetailsViewController: 0x7e6191a0.

and also viewController2 is left “orphaned” on the screen (there is no way to remove it)..

Here is code (in Xamarin c# but it the same problem in Objective C):

this.PresentViewController(viewController2, animated: true, completionHandler: null); // takes 0.2s to animate
//
// then 0.1s later, say, this is called:
//
this.DismissViewController(animated: false, completionHandler: null);
this.NavigationController.PopToRootViewController(animated: false);

The problem is that this.DismissViewController does not actually dismiss viewController2 while it is still animating.

(A similar problem occurs if a Push animation is in progress).

A solution is “queue” the request for PopToRootViewController and run it in the completionHandler for this.PresentViewController. But this is very cumbersome and means all events in the app have to be queued and all animations have to have completionHandler’s.

I really need a way to cancel the present animation (this.View.Layer.RemoveAllAnimations() doesn’t work for presentViewController) so that DismissViewController will then work and PopToRootViewController can be called without problems.

How do people deal with this "uncancellable animation" problem?

UPDATE:

Also there is a related problem now we have to use UIAlertController instead of UIAlert, see for background:

Show UIAlertController if already showing an Alert

We have to wait for any presentViewController animation to complete before presenting a UIAlertController. This is hard for things like a Push Notification which we want to show in an alert but it may arrive during an animation.

0

There are 0 answers