Boon
Boon

Reputation: 41510

How to preserve constraints for hidden views within UIStackView

I have three buttons within a horizontal stack view and I have set the autolayout up so they appear based on certain criteria.

For example, first button will be 40% of the stack view width, second button has the same width as the first button, and third button takes the remaining width. Height of the buttons are the same as the stack view. They are all spaced equally, 8 points apart. Stack view's distribution is set to to "equal spacing" and alignment is set to "center".

The issue appears when I decide to hide the first two buttons, but still would like the third button to appear in the same spot with the same width. Autolayout would resize the third button as if the first button and second button are not there.

How do I make it so the autolayout created continues to be preserved as if the hidden buttons are still there?

Upvotes: 1

Views: 1889

Answers (3)

Ammaiappan
Ammaiappan

Reputation: 539

I have one more better solution. Add the three UIView in stack align as per your need, then add the button in every UIView. Now you can play with button hidden true/false, here UIView will always be with clear color. Alignment won't change.

Upvotes: 0

chuckSaldana
chuckSaldana

Reputation: 1227

Use a second constraint to the container that can act as a backup constraint. Or just re-recreate the desired constraint via code.

Upvotes: 0

hhanesand
hhanesand

Reputation: 1000

Couldn't you just set the alpha to 0 and userInteractionEnabled to NO for the buttons you want to "hide"? This would cause them to disappear, and I don't think that it will cause the third button to expand.

Upvotes: 4

Related Questions