Structure of the cell is destroyed after UIView.transition

83 Views Asked by At

In one of my controllers I'm using UICollectionView with two-sided cells (there is a button on the face side of the cell, and if user is tapping it cell should show him/her the other side of the cell).

so I have this in my cell class:

firstView = UIView()
...
self.addSubview(firstView)

infoView = UIView()
...
self.insertSubview(infoView, belowSubview: firstView)

Both views have a lot of elements in them, for structuring these elements I'm using mostly Visual Format constraints and anchors.

i.e.

let topFirstViewConstraint = firstView.topAnchor.constraint(equalTo: self.topAnchor, constant: 4)
        allConstraints.append(topFirstViewConstraint)
...
let goButtonCenter = goButton.centerXAnchor.constraint(equalTo: firstView.centerXAnchor, constant: 0)
        allConstraints.append(goButtonCenter)

For switching between face and rear views I'm using UIView.transition:

UIView.transition(from: firstView, to: infoView, duration: 0.2, options: .transitionFlipFromLeft) { (success) in

        }

It works like a charm, I don't have any autoLayout errors at all.

But if I'm trying to switch from firstView to infoView firstView constraints goes wild and vice versa (I still don't have any errors but I know how these views should look, and it's a mess). I've tried to use self.updateConstraints() in transition closure but it doesn't work.

Did anyone experience something like that?

1

There are 1 best solutions below

1
On

https://developer.apple.com/documentation/uikit/uiview/1622562-transition says:

fromView: The starting view for the transition. By default, this view is removed from its superview as part of the transition.

What happens with constraints when one party of a constraint gets removed from superview? It definitely doesn‘t work anymore.

What you can try

Discussion: This method provides a simple way to transition from the view in the fromView parameter to the view in the toView parameter. By default, the view in fromView is replaced in the view hierarchy by the view in toView. If both views are already part of your view hierarchy, you can include the showHideTransitionViews option in the options parameter to simply hide or show them.