Why does a dismissed child UIView doesn't reappear properly after its first appearance?

Thanks to people on this board I am able to successfully calla up a child UIViewConroller via animation with:

This is the buttonAction from the Main UIViewController, which calls up setController

    @objc func buttonAction(sender: UIButton!) {
        guard let theButton = sender as? MyButton else { return}
        
        UIView.transition(with: self.view, duration: 0.5, options: .transitionCurlDown, animations: { [self] in
            
            self.addChild(setController);
            self.view.addSubview(setController.view);

        }, completion: { [self]_ in setController.didMove(toParent: self);
        							setController.doLayout();})
    }

the doLayout method lies within the child:

        func doLayout (){
        guard let parent = cView!.view.superview else {return}
        
        //make sure UIV honors safeAreaLayouts
        setConstraints(vc: self, pc: parent)
    }

A button within the child, setController, dismisses itself:

    @objc func buttonAction(sender: UIButton!) {
        self.willMove(toParent: nil)
        self.removeFromParent()
        self.view.removeFromSuperview()
        self.dismiss(animated: false, completion: nil)
    }

Everything works great the first time I call up the child UIView. It curls down while covering the first/parent UIVIEW, etc. etc. Figure 1 But after I dismiss the child view and call it again, the child view scrolls down without really covering the main view, it's like a mishmash. Figure 2 Only after all is said and done, then the child view covers everything.

So am curious if I am dismissing something incorrectly.

There are visibly some issues with curl when dismissing.

In some cases, animation doesn't work.

https://stackoverflow.com/questions/62187639/swift-modal-transition-partial-curl-on-dismiss-got-stuck

Could you test with another type of animation: transitionFlipFromLeft

Experimenting around I found that if I didn't set the child's constraints (see below), then it would always work.

Is there a method to remove the constraints of a UIView before deleting it?

vc is the Child UIView and pc is its parent

func setConstraints (vc: UIViewController, pc: UIView) {
    vc.view.translatesAutoresizingMaskIntoConstraints = false
    var constraints = [NSLayoutConstraint]()
    constraints.append(vc.view.leadingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.leadingAnchor))
    constraints.append(vc.view.trailingAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.trailingAnchor))
    constraints.append(vc.view.bottomAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.bottomAnchor))
    constraints.append(vc.view.topAnchor.constraint(equalTo: pc.safeAreaLayoutGuide.topAnchor))
     NSLayoutConstraint.activate(constraints)
}

To remove all constraints on a view:

func removeAllConstraintsFromView(view: UIView) { 
   for c in view.constraints { 
       view.removeConstraint(c) 
   } 
}
Why does a dismissed child UIView doesn't reappear properly after its first appearance?
 
 
Q