简体   繁体   English

如何在 UIStackView 中保留隐藏视图的约束

[英]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.例如,第一个按钮将是堆栈视图宽度的 40%,第二个按钮与第一个按钮具有相同的宽度,第三个按钮占用剩余的宽度。 Height of the buttons are the same as the stack view.按钮的高度与堆栈视图相同。 They are all spaced equally, 8 points apart.它们都等距,相隔 8 点。 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?如何使创建的自动布局继续保留,就好像隐藏的按钮仍然存在一样?

Couldn't you just set the alpha to 0 and userInteractionEnabled to NO for the buttons you want to "hide"?您不能将要“隐藏”的按钮的alpha设置为0并将userInteractionEnabled设置为NO吗? This would cause them to disappear, and I don't think that it will cause the third button to expand.这会导致它们消失,我不认为它会导致第三个按钮展开。

Use a second constraint to the container that can act as a backup constraint.对可充当备份约束的容器使用第二个约束。 Or just re-recreate the desired constraint via code.或者只是通过代码重新创建所需的约束。

I have one more better solution.我还有一个更好的解决方案。 Add the three UIView in stack align as per your need, then add the button in every UIView.根据需要在堆栈对齐中添加三个 UIView,然后在每个 UIView 中添加按钮。 Now you can play with button hidden true/false, here UIView will always be with clear color.现在您可以使用隐藏按钮的真/假,这里的 UIView 将始终具有清晰的颜色。 Alignment won't change.对齐方式不会改变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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