6
votes

Given the following view controller layout.

View Controller Layout

We build a stack of modal view controllers by first presenting B on A and then presenting C on B. According to the Apple documentation on dismiss(animated:completion:), calling it on A should actually dismiss the topmost view controller (C in this case) in an animated fashion and all intermediate view controllers without animation. What happens though is that C gets dismissed without animation and B is dismissed in an animated fashion.

I put up an Xcode project on GitHub that replicates that behaviour. Am I missing something or am I misunderstanding the documentation here?

3
Did you ever figure this out? I just ran into it and am pretty frustrated. It seems as if this behavior has been around for several versions of iOS and is still present in iOS 10. This goes directly against the documented behavior (and common sense) that you would animate the TOP most view controller and not the middle ones.MobileVet
No I am still facing the issue. Maybe it's time to file a radar ...mAu
Just found one... it was duplicated and then closed due to inactivity. Also found a test project that quickly demonstrates the issue. Apple thought they resolved it in 8.3 but the user never updated so they closed the bug... but it was still present and still is today.MobileVet
I just submitted a bug report #28713792 to Apple. I included the test project that someone else was nice enough to create so hopefully it is easily understood.MobileVet
Will dupe that. Thanks for your effort!mAu

3 Answers

7
votes

After poking around the web and trying out various 'solutions' it is clear this is an actual bug within iOS. It has been present since iOS 8... and is still present in iOS 10. It was originally reported in iOS 8, but the solution was never validated and Apple automatically closed the radar due to inactivity.

I have filed a new radar as this is in direct contradiction to the documentation for dismissViewController

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method(means -[UIViewController dismissViewControllerAnimated:completion]) on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack.

Clear visualization of the issue, both expected and actual results. Credit to Boris Survorov for the test project and visualizations.

Expected ResultsActual Results iOS 8+

4
votes

I've experienced the same issue and here is what I've found to be a viable workaround. When you need to dismiss the whole stack, execute this code in A:

viewControllerB.view.isHidden = true
viewControllerC.dismiss(animated: true) // or viewControllerB.dismiss(animated:true) - it should produce the same result: dismiss viewControllerC
dismiss(animated: false) // dismisses viewControllerB

This should result with the expected behavior.

0
votes

I am guessing that your segue from A to B is modal as well? In that case the dismiss function called from A wants to dismiss the view, which is immediately on top of A, which is B. C just gets hidden in order to show you the animated hiding of B. In that sense you cannot stack views via modal segues and dismiss the top one with the dismiss function as you described if you go that far back. The dismiss would work as intended if called from B to dismiss C though.