简体   繁体   English

从视图中删除第三个按钮时如何增加两个按钮的宽度

[英]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: 您有几种选择:

  1. UIStackView which was made exactly for this. UIStackView正是为此而制作的。
  2. UICollectionView similar to UIStackView in a certain way, but not really meant for this. UICollectionViewUICollectionView类似于UIStackView ,但这并不是真的。 However, it does the job nicely and it's easy to implement. 但是,它可以很好地完成工作,并且易于实现。 Sometimes easier than UIStackView . 有时比UIStackView容易。
  3. 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. 当堆栈视图内的UIViewisHidden属性设置为true时,该堆栈视图将隐藏该视图并处理布局,因此您只需为堆栈视图设置正确的约束即可。

暂无
暂无

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

相关问题 当我们在for循环内创建Button框架时,现在我们要删除或清理按钮框架,那该怎么办呢? - When we create Button frame within for loop then now we want to remove or clean button frame then how can do this? 如何使用Swift在集合视图上添加按钮? - How can we add a button over collection view using swift? 我们可以在滚动视图上添加按钮吗 - Can we add button on scroll view 如何从View中删除Button - How to remove Button from View 当我们在iOS中获取Facebook用户详细信息时如何删除注销按钮标题 - How to remove logout button title when we get facebook user details in ios 在我们在警报视图文本字段中编写时,在iOS中从SCLAlertView创建一个按钮 - To make a button from SCLAlertView in iOS as we write in alert view textfield 当我们点击它时,如何更改添加在表格视图单元格上的按钮背景色? - How to change button background color which is added on table view cell when we tapped on it? 当我们单击按钮时如何更改图像? - How to change a image when when we click a button? 如果我们将swrevealview用作初始视图控制器,如何将swrevealview设置为第三视图控制器 - how can i place swrevealview as a third view controller,if we make swrevealview as a initial view controller its working Objective C如何在单击按钮后又恢复正常状态时更改按钮的背景图像? - Objective C How can we change the background image of a button in just when it is clicked and then go back to normal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM