Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animating UIStackView arrangedSubview content size change

Is it possible to animate an arranged subview when its intrinsic content size changes?

For example, imagine I have an arranged subview, which holds a single UILabel pinned to edges. That label has a small amount of text. New text comes in, which is larger than the previous text. The intrinsic content size of label is now larger.

I would like to be able to animate it like so:

 stackView.layoutIfNeeded()
 self.label.text = self.expanded ? textTwo : textOne
 UIView.animateWithDuration(0.3) { () -> Void in
     self.stackView.layoutIfNeeded()
 }

The stackview currently "jumps" from the old content size to the new one, ignoring the animation block.

An alternative of course is to manually find out the height of the arranged subview, and animate that constraint. I'd like to avoid that if possible.

like image 831
Christopher Kevin Howell Avatar asked Feb 12 '16 14:02

Christopher Kevin Howell


1 Answers

Found something that works:

label.text = expanded ? textOne : textTwo
UIView.animateWithDuration(0.4) { () -> Void in
    self.stackView.setNeedsLayout()
    self.stackView.layoutIfNeeded()
}

Does not work:

label.text = expanded ? textOne : textTwo
UIView.animateWithDuration(0.4) { () -> Void in
    self.stackView.layoutIfNeeded()
}

Strange that changing of the intrinsic content size doesn't make the stackView want to need layout though...

like image 78
Christopher Kevin Howell Avatar answered Jan 15 '23 15:01

Christopher Kevin Howell