简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM