Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Direction of animation when un-hiding a UIStackView element

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?

like image 610
Christopher Simmons Avatar asked Nov 23 '15 02:11

Christopher Simmons


2 Answers

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)
like image 154
jamesk Avatar answered Sep 19 '22 12:09

jamesk


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.

like image 26
shadowhorst Avatar answered Sep 19 '22 12:09

shadowhorst