[英]How we can increase width of two button when we remove third button from view
I have view with three button with equal size. 我有三个大小相等的按钮。 Each button take 1/3 part portion of view.
每个按钮占视图的1/3。
Like this image: 喜欢这张图片:
If I remove/hide one button then two button width should increase equally and take 1/2 portion of view. 如果我删除/隐藏一个按钮,则两个按钮的宽度应相等地增加,并占据1/2的视角。 if I remove two button then one button size should be equal size of view.
如果我删除两个按钮,则一个按钮的大小应等于视图的大小。
My question is, how it's possible using the Autolayout. 我的问题是,如何使用自动版式。
Best option is using stackView. 最好的选择是使用stackView。 StackView gives lots of flexibility in adding or removing items.
StackView在添加或删除项目方面具有很大的灵活性。 If you wish to use only auto layouts, you can achieve it by connect it's width constraints as IBOutlet and change the values programatically.
如果只希望使用自动布局,则可以通过将其宽度约束作为IBOutlet连接并以编程方式更改值来实现。
Best way to do that is to use UISTACKVIEW.Place a stackview and add 3 buttons.You can give proper layout constraints to the stack view as you need 最好的方法是使用UISTACKVIEW。放置一个堆栈视图并添加3个按钮。您可以根据需要为堆栈视图提供适当的布局约束
click on stack view-- select attribute inspector change distribution--fill equally spacing--0 单击堆栈视图-选择属性检查器更改分布-等距填充--0
Then after that if you hide any button,other buttons will be automatically adjusted in width 然后,如果您隐藏任何按钮,其他按钮的宽度将自动调整
Other Possible sol to this problem is Adding or removing constraints during runtime is a heavyweight operation that can affect performance. 解决此问题的其他可能办法是,在运行时添加或删除约束是一项重量级的操作,可能会影响性能。 However, there is a simpler alternative.
但是,有一个更简单的选择。
For the view you wish to hide, set up a width constraint. 对于要隐藏的视图,设置宽度约束。 Constrain the other views with a leading horizontal gap to that view.
将其他视图限制在该视图的前导水平间隙处。
To hide, update the .constant of the width constraint to 0.f. 若要隐藏,请将宽度约束的.constant更新为0.f。 The other views will automatically move left to assume position.
其他视图将自动向左移动到指定位置。 and for equal width pervoid multiplier to width..
对于相等的宽度,请避免乘以宽度。
You have a few options: 您有几种选择:
UIStackView
which was made exactly for this. UIStackView
正是为此而制作的。 UICollectionView
similar to UIStackView
in a certain way, but not really meant for this. UICollectionView
在UICollectionView
类似于UIStackView
,但这并不是真的。 However, it does the job nicely and it's easy to implement. UIStackView
. UIStackView
容易。 NSLayoutConstraint
by using multiple constraints with different priority so that you can activate/deactivate them as needed and get the desired result. NSLayoutConstraint
通过使用具有不同优先级的多个约束,以便您可以根据需要激活/停用它们并获得所需的结果。 This approach is a bit more complex by it gives you the highest degree of control and flexibility over the views in your hierarchy. The best way to achieve what you are looking for is, like others have already mentioned, to use a UIStackView
. 就像其他人已经提到的那样,实现您正在寻找的最佳方法是使用
UIStackView
。
When the isHidden
property of a UIView
inside a stack view is set to true, that stack view will hide the view and take care of the layout, so you will only need to set the correct constraints for your stack view. 当堆栈视图内的
UIView
的isHidden
属性设置为true时,该堆栈视图将隐藏该视图并处理布局,因此您只需为堆栈视图设置正确的约束即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.