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

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

问题描述

我只想让我的按钮具有彼此相等的空间和固定的尾随和前导空格。
我有点自动布局noob,所以我没有任何线索如何解决这个问题。我尝试了很多不同的配置,但没有什么工作,因为它应该。任何想法如何解决这个问题?
这将是真棒。



解决方案

要根据设备的方向布局按比例间隔的多个视图,之间的可见视图。正确设置这些分隔符视图的约束,以确保可见视图能够根据设备的方向保持分隔。



步骤:


  1. 创建可见视图。



  2. 要放置两个可见视图,请按照以下格式放置所有视图,从屏幕左侧开始向右移动:


spacer1 | view1 | spacer2 | view2 | spacer3。


  1. 限制间距视图,使其长度彼此相等。


  2. 创建从第一个分隔符视图到容器视图的前导约束。


  3. 在分隔视图和可见视图之间创建约束。

    b

以下示例使用上述任务中的步骤来说明如何按比例间隔定位两个视图。间隔物视图被注释为示例,但通常留为空而没有背景。首先,创建两个视图,并将它们放置在故事板中。





添加三个分隔符视图 - 一个在左侧视图的左侧,一个在两个视图之间,一个在右侧最右边的视图。此时间隔视图不必具有相同的大小,因为它们的大小将通过约束来设置。





为分隔符视图创建以下约束:


  1. 限制间隔视图2和间距视图3的宽度等于间距视图1的宽度。

  2. 约束

  3. 创建从间隔视图1到容器的前导空间到容器约束。

  4. 从间隔视图1到视图1创建水平间距约束。将此约束设置为优先级为1000的小于或等于约束。

  5. 从间隔视图2到视图1和视图2创建水平间距约束。将这些约束设置为优先级为999的小于或等于约束。

  6. 创建从间隔视图3到视图2的水平间距约束。将此约束设置为优先级为1000的约束。

  7. 创建尾随



    这些约束创建两个可见视图和三个不可见视图(间隔视图)。这些分隔视图会随着设备方向的变化而自动调整大小,如下图所示:






这是苹果的方式也建议在此示例



除此之外,您可以在按钮容器视图中执行这些操作,在视图中对齐按钮容器视图,并保持前导和尾随约束与相同的常量值具有相等的关系。



我已经给出了这里的示例的相同步骤,因为根据链接的回答可能不是优选的。

/ p>

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

Steps:

  1. Create the visible views.

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

  3. Alternate placing your views, starting with a spacer view.

  4. 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 | view1 | spacer2 | view2 | spacer3.

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

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

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

  4. 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. 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:

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.

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

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

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