Dismissing modal view controller stack

后端 未结 3 461
日久生厌
日久生厌 2021-01-05 14:43

Given the following view controller layout.

We build a stack of modal view controllers by first presenting B on A and then present

相关标签:
3条回答
  • 2021-01-05 15:12

    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.

    0 讨论(0)
  • 2021-01-05 15:28

    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.

    0 讨论(0)
  • 2021-01-05 15:35

    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 讨论(0)
提交回复
热议问题