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.


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...

链接地址: http://www.djcxy.com/p/32190.html

上一篇: 如何使用UIImagePNGRepresentation压缩图像大小

下一篇: 动画UIStackView排列的子视图内容大小更改