swiftuiviewcontrollerdismissremovefromsuperview

Dismiss completly a view controller in middle


I've been looking for days. Here's the problem: I have a Home view controller. When it receives a notification, it presents a NotiViewController, called noti1, by

`home.present(noti1, animated: true, completion: nil)`. 

After 15sec, another notification comes, I get the top view controller topVC, present another NotiViewController, called noti2, by

`topVC.present(noti2, animated: true, completion: nil)`.

Each notiVC has an timer which waits for 30sec to dismiss itself. Now I have Home -> noti1 -> noti2.

After 15sec, noti1 runs out of time and has to be dismissed. How can I dismiss it without interupting noti2 which is being presenting?

I tried

`orderVC.beginAppearanceTransition(false, animated: false)
 orderVC.willMove(toParent: nil)
 orderVC.view.removeFromSuperview()
 orderVC.removeFromParent()
 orderVC.endAppearanceTransition()`

These code does remove the view from screen, but it leaves a UITransitionView behind, which blocks user actions.

Blocking UITrainstionView(s)

This image is took after noti2 is removed from superview etc, so there are 2 UITransitionViews is presenting, blocking user actions.

Any idea? Thanks a lot.


Solution

  • Found a way. I'm not sure if I'm correct or wrong, but here is my thought: View controller must be dismissed the way it's presented before. For example if you use present(viewController, animated, completion) to present, you should use dismiss(animated, completion) to dismiss. The problem, in my case, is if you dismiss a view controller, all child view controllers will be gone too.

    The way I resolved my issue is using another way to "show" the target view controller: parentVC.view.addSubview(childVC.view) parentVC.addChild(childVC) childVC.didMove(toParent: parentVC)

    After adding some view controllers, I got a tree parentVC -> child1 -> child2 -> child3... If I want to dismiss child2, the code in my question worked: child2.willMove(toParent: nil) child2.view.removeFromSuperview() child2.removeFromParent()

    There is no UITransitionView blocking user actions, but it got no animation. For my this is good enough.