0
votes

I have an UIStackView build in interface builder which changes it's axis property from vertical to horizontal when it's superview rotates from portrait to landscape. The issue is that this results in an animation of it's two arranged subviews, where one of those views overlaps the other view while transitioning to the new position.

In my case I would like to remove only that particular animation and force the view to it's end position right away.

I have tried to remove all animation on from the stackview layer in my VC:

public override func willTransition(to newCollection: UITraitCollection,
                                    with coordinator: UIViewControllerTransitionCoordinator) {

    coordinator.animate(alongsideTransition: {
        _ in
        // The first subview is `UIStackView`           
        self.view.subviews[0].layer.removeAllAnimations()
    })

    super.willTransition(to: newCollection, with: coordinator)
}

And I observed the animation keys when the axis property is changed, because that seems to happen somewhere during the transition (set automatically by interface builder). Unfortunately I had no luck with it, because the layer returns nil for animationKeys on the stackview and its subviews/arrangedSubviews.

class StackView : UIStackView {

    override var axis: UILayoutConstraintAxis {

        didSet { /* here */ }
    }
}
1

1 Answers

0
votes

That's going to be challenging, I think. There's a nice demo of what it sounds like you're trying to do over here if the axis animation it shows is what you're trying to get rid of, pretty sure that's outside the UIStackView design space.

Pretty sure the best solution to "force the view to it's end position right away" is to in fact have one UIStackView for each axis and move the contents between them on rotation.

You could also try putting a performWithoutAnimation block in your viewWillTransition(to:with:) that calls updateConstraintsIfNeeded on completion? Maybe that would work. I'd go with the two-view idea instead of that though!