I have the a red color stack view.
It contains a label and a purple custom view.
It looks as following

What I would like to achieve is
I use the following code
class ViewController: UIViewController {
@IBOutlet weak var purpleView: UIView!
@IBOutlet weak var stackView: UIStackView!
@IBOutlet weak var purpleViewHeightConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
var value: CGFloat = 200.0
@IBAction func buttonClicked(_ sender: Any) {
if value == 500 {
value = 200.0
} else {
value = 500.0
}
UIView.animate(withDuration: 2) {
self.purpleViewHeightConstraint.constant = self.value
self.purpleView.setNeedsLayout()
self.purpleView.layoutIfNeeded()
}
}
}
This is what I have achieved.

We can observe
Without removing UIStackView, how can I achieve?
I post the demo project - https://github.com/yccheok/animate-stack-view-item
Thanks.
Set your button action like this
@IBAction func buttonClicked(_ sender: Any) {
if value == 500 {
value = 200.0
} else {
value = 500.0
}
self.purpleViewHeightConstraint.constant = self.value
UIView.animate(withDuration: 2) {
self.view.layoutIfNeeded()
}
}

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With