When I take a UIView that is an element of a vertical stack view and change that element's "hidden" property from true to false, the change is automatically animated, and the element moves downward as it becomes visible. I want it to move upward instead. Is there a way to do that?
The UIStackViewDistribution
enum in the UIStackView header file is accompanied by the following comment for the fill
case:
When items do not fit (overflow) or fill (underflow) the space available adjustments occur according to compressionResistance or hugging priorities of items, or when that is ambiguous, according to arrangement order.
You can affect the appearance of item animations by setting the content compression resistance and content hugging priorities of the items.
So, for example, when hiding and showing labels in a vertical stack view with a fill
distribution, you can make each label shrink and grow from its position in arrangedSubviews
by ensuring it has a required content hugging priority for the vertical axis:
label.setContentHuggingPriority(UILayoutPriorityRequired, for: .vertical)
I just answered to an similar question here: Animate hidden property on UILabels in UIStackView causes different animations
Setting the UIView
's contentMode
property seems to affect the way the UIView
animation inside an UIStackView
is performed.
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