Search code examples
iosswiftuistackview

UIStackView - Hide/Unhide animation not as expected


I am trying to perform smooth animation when hiding and unhiding UI elements in a vertical StackView. When the UI element has height constraint set, I am unable to obtain expected result. The view does not hide rapidly alongside the animating stackView.

Below is the button action

@IBAction
func didSelect(_ sender: UIButton) {
    UIView.animate(withDuration: 3) {
        self.viewToHide.isHidden.toggle()
    }
}

and Storyboard

enter image description here

enter image description here

The green view is expected to update animate along with the animating stackView. But, it hides after completing the animation and unhides instantly as animation starts when toggled. What am I missing here?


Solution

  • You need to reduce viewToHide constraint priority to make it work. For example:

    enter image description here

    Output:

    enter image description here