xcode 并排排列 3 个 UIButtons(等宽)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36170982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Arrange 3 UIButtons(of equal width) side-by-side
提问by UdayM
How to arrange 3 UIButtons side-by-side.I am using Auto-layout. My requirement is:
如何并排排列 3 个 UIButton。我正在使用自动布局。我的要求是:
1.they should be equal width regardless of device
1.无论设备如何,它们都应该是等宽的
2.occupy vertically from starting to end of the view
2.从视图的开始到结束垂直占据
i tried different ways, i failed to achieve that. Is it possible through interface builder
我尝试了不同的方法,但未能实现。是否可以通过界面构建器
回答by Jatin Chauhan
There are 2 ways to do this.
有两种方法可以做到这一点。
Way 1:
方式一:
With use of equal width constraint of button
使用按钮的等宽约束
Select all 3 buttons and add
选择所有 3 个按钮并添加
top, left , right, height and equal width
上、左、右、高和等宽
Way 2:With use of Stack view
方式2:使用堆栈视图
Step 1:Add 3 buttons.
步骤1:添加3个按钮。
Step 2:Select all that buttons, Once you selected, click on the Stack buttonin the Auto Layout toolbar at the bottom right of the storyboard canvas. see below in image.
第 2 步:选择所有按钮,选择后,单击故事板画布右下角自动布局工具栏中的堆栈按钮。见下图。
Alternatively you can embed in FromEditor -> Embed in -> StackView
或者,您可以嵌入 From Editor -> Embed in -> StackView
Step 3:Add Constraintsto StackView. like below.
Step 4:Select StackView, Once selected go to Attributes inspector. Change the Distributionto Fill Equally:
第 4 步:选择 StackView,选择后转到Attributes inspector。更改分布以均匀填充:
And its Done!
大功告成!
回答by EI Captain v2.0
Select 3 buttons and give this constraints
选择 3 个按钮并给出这个约束
- leading
- trailing
- bottom
- Height
- equal width
You can check some references from equal width to 3 labelsand from setting two buttons of equal width, side by side
您可以检查一些参考,从等宽到 3 个标签,以及并排设置两个等宽的按钮
回答by MrDank
It is possible through interface builder. This is what you do
可以通过界面构建器。这是你做的
Create a UIViewController from your Storyboard by clicking the Object Library button.
Now select and drag three buttons and add them to your View Controller. Be sure to add the buttons through here. Drag and drop in the View of your View Controller. Be sure to place the buttons at the bottom of your View Controller.
After your three buttons are added , it should look something like this
Now comes the interesting part. Now what you need to do is add constraints to your buttons so that they stay always at the bottom and of equal width. For simplicity let me call the three buttons as left , center and right button. To keep the button at the button , select the center button and press CONTROL on your keyboard and drag the button to the View. Select the constraint
现在选择并拖动三个按钮并将它们添加到您的视图控制器。请务必通过此处添加按钮。拖放到您的视图控制器的视图中。确保将按钮放在视图控制器的底部。
现在是有趣的部分。现在您需要做的是为按钮添加约束,使它们始终位于底部且宽度相等。为简单起见,我将这三个按钮称为左、中和右按钮。要将按钮保持在按钮 ,请选择中心按钮并按键盘上的 CONTROL 并将按钮拖到视图中。选择约束
Vertical spacing to Bottom Layout Guide
底部布局指南的垂直间距
and keep a constant = 0. Now for the other two buttons you do this. Simply press CONTROL and drag to the central button. Select the constraint
并保持一个常数 = 0。现在为其他两个按钮执行此操作。只需按下 CONTROL 并拖动到中央按钮即可。选择约束
Bottom
底部
and keep constant for that constraint as 0.This ensures that all the buttons stay at the bottom.
并将该约束的常量保持为 0。这确保所有按钮都位于底部。
Add the Height constraint as much as you want for all the three buttons.
根据需要为所有三个按钮添加高度约束。
At the end, these are the constraints you must have.
最后,这些是您必须具备的约束条件。
Center Button Constraint
中心按钮约束
Left Button Constraint
左按钮约束
Right Button Constraint
右键约束
Now all you need to do is Control+drag the Width constraint of your center button to your ViewController.swift file and change the constant of the width to
widthConstraint.constant = UIScreen.mainScreen().bounds.width/3
现在你需要做的就是 Control+drag 你的中心按钮的 Width 约束到你的 ViewController.swift 文件并将宽度的常量更改为
widthConstraint.constant = UIScreen.mainScreen().bounds.width/3
And that's it.
就是这样。
回答by Ujjwal
Set the constraints as following :
设置约束如下:
- Select all button and set the equal width constraint, this constraint will keep the width of all the buttons same.
- Select all buttons and set the bottom margin, this will keep all the button at bottom of the screen in all form factors.
- Set the left margin and right margin of first and last button respectively, with the superview.
- Select the button in the middle and set left margin and right margin constraint.
- 选择所有按钮并设置等宽约束,此约束将保持所有按钮的宽度相同。
- 选择所有按钮并设置底部边距,这将使所有按钮以所有形式保持在屏幕底部。
- 用superview分别设置第一个和最后一个按钮的左边距和右边距。
- 选择中间的按钮并设置左边距和右边距约束。