如何安排使用自动布局UIButtons [英] How to arrange UIButtons using Autolayout

查看:118
本文介绍了如何安排使用自动布局UIButtons的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只希望我的按钮有平等的空间,彼此固定的尾随和前导空格。
我还挺自动布局小白,所以我没有任何线索如何解决这个问题。我尝试了很多不同的配置,但没有工作,因为它应该。任何想法如何解决这个问题呢?
这将是真正真棒。

I just want my buttons to have equal spaces to each other and a fixed trailing and leading space. I am kinda Autolayout noob, so i dont have any clue how to solve this problem. I tried a lot of different configurations but nothing worked as it should. Any ideas how to solve this Issue ? This would be really awesome.

推荐答案

要制定出了基于设备的方向按比例隔开的多个视图,创建可见视图之间间隔的意见。正确设置这些间隔的意见约束,以确保在可见的意见能够保持间隔根据设备的方向

To lay out several views that are proportionally spaced based on the orientation of a device, create spacer views between the visible views. Set the constraints of these spacer views correctly to ensure that the visible views are able to stay spaced apart based on the orientation of the device

步骤:


  1. 创建可见的看法。

  1. Create the visible views.

创建间隔的意见等于可见视图加一的数量。

Create the spacer views equal to the number of visible views plus one.

交替放置您的看法,与间隔视图开始。

Alternate placing your views, starting with a spacer view.

要空间两个可见的意见,把所有的意见,以下模式,从屏幕左侧开始并向右移动:

To space two visible views, place all of the views in the following pattern, starting from the left side of the screen and moving right:

spacer1 |厂景| spacer2 |视图2 | spacer3。

spacer1 | view1 | spacer2 | view2 | spacer3.


  1. 约束隔板的观点,使它们的长度是彼此相等的。

  1. Constrain the spacer views so that their lengths are equal to each other.

从第一间隔视图容器视图中创建一个领先的约束。

Create a leading constraint from the first spacer view to the container view.

创建从上次间隔视图容器视图尾随约束。

Create a trailing constraint from the last spacer view to the container view.

创建间隔的意见和看法可见之间的约束。

Create constraints between the spacer views and the visible views.

下面的示例使用上述任务中的步骤来展示如何定位按比例隔开的两个视图。间隔件次被注释为示例,但通常保留空没有背景。首先,创建两个视图,并放置在故事板。

The following example uses the steps in the above task to show how to position two views proportionally spaced. The spacer views are annotated for the example, but are normally left empty with no background. First, create the two views and place them in the storyboard.

视图模式-添加三垫片最左边的视图,一个在两个视图之间,和一个到最右边的视图的右边的左边。间隔件次不必具有相同的大小,此时,因为它们的尺寸将通过限制被设置

Add the three spacer views—one to the left of the leftmost view, one between the two views, and one to the right of the rightmost view. The spacer views don’t have to be the same size at this time because their size will be set through constraints.

有关隔离意见创建以下限制:

Create the following constraints for the spacer views:


  1. 约束间隔视图2和间隔视图3的宽度等于间隔视图1的宽度。

  2. 约束垫片视图1的宽度为大于或等于最小所需的​​宽度。

  3. 创建从隔离视图1到容器一个空格,集装箱约束。

  4. 创建从隔离视图1水平间距约束视图1.设置这个约束是一个低于或相等,与1000优先约束。

  5. 创建从隔离视图2水平间距约束视图1和视图2.设置这些限制是一个低于或相等,与999优先约束。

  6. 创建从隔离视图3水平间距约束视图2.设置这个约束是一个低于或相等,与1000优先约束。

  7. 创建间隔视图3个空格,以集装箱约束到容器中。

  1. Constrain the width of spacer view 2 and spacer view 3 to be equal to the width of spacer view 1.
  2. Constrain the width of spacer view 1 to be greater than or equal to the minimum desired width.
  3. Create a Leading Space to Container constraint from spacer view 1 to the container.
  4. Create a Horizontal Spacing constraint from spacer view 1 to view 1. Set this constraint to be a less-than-or-equal-to constraint with a priority of 1000.
  5. Create Horizontal Spacing constraints from spacer view 2 to view 1 and view 2. Set these constraints to be a less-than-or-equal-to constraint with a priority of 999.
  6. Create a Horizontal Spacing constraint from spacer view 3 to view 2. Set this constraint to be a less-than-or-equal-to constraint with a priority of 1000.
  7. Create a Trailing Space to Container constraint from spacer view 3 to the container.

这些约束创建两个可见视图和三个看不见的意见(间隔意见)。这些间隔的意见会自动调整尺寸的设备更改方向,保持按比例隔开的可见​​视图,如在以下两个数字:

These constraints create two visible views and three invisible views (spacer views). These spacer views automatically resize as the orientation of the device changes, keeping the visible views proportionally spaced, as shown in the following two figures:

这是苹果的方式还建议在<一做href=\"https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/AutoLayoutbyExample/AutoLayoutbyExample.html#//apple_ref/doc/uid/TP40010853-CH5-SW8\"相对=nofollow>这个例子

This is the way apple also suggests to do in this example

除了这个,你做这些事情的按钮容器视图是有那些按键和调整你的观点,即按钮容器视图,并保持领先,并用相同的恒定值相等关系尾随约束。并加入剩余的顶部和底部限制那些可能需要的。

In addition to this, You do these things in a buttons container view which is having those buttons and align that button container view in your view and keep leading and trailing constraints with equal relationship with same constant value. And add the remaining top and bottom constraints as those may be needed.

我给这里的例子同样的步骤,因为这取决于链接的答案可能不是preferable。

I have given same steps of the example here, because answer depending on links may not be preferable.

这篇关于如何安排使用自动布局UIButtons的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆