自动布局中的弹簧:在Xcode 5中均匀分布视图,使用约束 [英] Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5

查看:489
本文介绍了自动布局中的弹簧:在Xcode 5中均匀分布视图,使用约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解在Interface Builder中调整,调整和分布视图的旧Struts和Springs方法。但是,我似乎不知道如何使用自动布局与Xcode 5均匀分布视图。有一种方法使用Xcode 4,但该选项已过。



我有7个按钮排列在垂直堆栈。在3.5英寸的布局,它看起来不错,当我在4布局预览屏幕,所有的按钮保持紧凑包装,并在最后一个按钮下面有大量的空间。



我希望他们保持相同的高度,但我想让他们之间的空间能够flex,所以他们可以跨屏幕展开。



>



我已经能够得到按钮的高度来弯曲和填充空间,但这不是我想要的行为。我想学习如何使用Auto Layout来代替我的旧Springs行为,但是我似乎没有找到任何方法通过Interface Builder来做。



我确定,顶部按钮或者是从顶部边缘的固定空间,或者是与顶部边缘成比例的空间,同样对于底部按钮和底部边缘。这些对我来说不太重要,我对任何一个都很好。



但我真的需要弄清楚如何均匀分配每个项目之间的额外空间

请注意,在iOS 9中,这种技术将不再需要,因为UIStackView将会自动执行分发。我将添加另一个答案,说明其工作原理。



如何使用Autolayout执行均匀分布



在Interface Builder中单独执行此操作(而不是在代码中构造约束)的最简单的方法是使用spacer视图:


  1. 绝对放置顶部和底部按钮。


  2. 间隔视图之间所有的按钮。


  3. 在每个按钮和其上方和下方的间隔视图之间进行约束。使用约束将它们水平定位(水平居中对其最简单) ,常数为0.


  4. 现在选择所有分隔符视图并将其高度设置为相等。


第一个屏幕截图显示我在IB中设置此项:





我故意不修正错放的观看次数,因为我想让你看看它在我设计约束时的样子。以下是4英寸和3.5英寸屏幕上的结果:





我已经离开隔离器视图黑色,只是为了告诉你这种技术如何工作,但当然在现实生活中,他们透明,因此看不见!所以用户只看到你的按钮,均匀分布在屏幕的任何一个高度。



使用这种技术的原因是,虽然相等的概念执行分布您所要求的值,约束可以仅在视图的各方面之间应用等式;因此我们需要额外的视图(间隔视图),这样我们就可以使其他事情(这里是间隔视图的高度)相等。



其他方法



显然,更灵活的方法是在代码中分配约束。这可能听起来令人生畏,但有很多第三方代码可以帮助您,例如这一类



例如,如果我们有一个(可能是不可见的)超级视图,其高度作为边界来规定我们的四个按钮的最大垂直分布,我们可以使用 0 常量将顶部固定到该超级视图的垂直中心,但是乘数 0.000001 0.666667 1.33333 2.0 (如果我们有四个按钮);现在的按钮将保持垂直分布,即使超级更改大小响应屏幕高度或任何。 [在Xcode 5.1中,可以在Interface Builder中设置,但在Xcode的早期版本中是不可能的。]


I understand the old Struts and Springs method of aligning, sizing and distributing views in Interface Builder. However, I cannot seem to figure out how to evenly distribute views using auto layout with Xcode 5. There was a way to do it using Xcode 4, but that option is gone.

I have 7 buttons arranged in a vertical stack. On a 3.5" layout, it looks great. When I preview the screen in the 4" layout, all of the buttons remain tightly packed and there is a large amount of space below the last button.

I want them to stay the same height, but I want the space between them to be able flex so they can spread out across the screen.

I've been able to get the height of the buttons to flex and fill the space, but that is not my desired behavior. I would like to learn how to use Auto Layout to replace my old Springs behavior, but I can't seem to find any way to do it through Interface Builder.

I'm ok with the top button either being a fixed space from the top edge or a proportional space from the top edge, likewise for the bottom button and the bottom edge. Those are less important to me, I'm good with either.

But I really need to figure out how to evenly distribute the extra space between each of the items in the view.

解决方案

EDIT Note that in iOS 9 this technique will become unnecessary, because a UIStackView will perform distribution automatically. I'll add another answer explaining how that works.

How to Perform Even Distribution Using Autolayout

The simplest way to do this in Interface Builder alone (rather than constructing constraints in code) is to use "spacer" views:

  1. Position the top and bottom buttons absolutely.

  2. Place spacer views between all the buttons. Use constraints to position them horizontally (centering them horizontally is simplest) and to set their widths.

  3. Make constraints between each button and the spacer view above and below it, with a Constant of 0.

  4. Now select all the spacer views and set their heights to be equal.

The first screen shot shows me setting this up in IB:

I have deliberately not corrected for the "misplaced views" because I want you to see what it looks like while I'm designing the constraints. Here's the result on both a 4 inch and a 3.5 inch screen:

I have left the spacer views black, just to show you how this technique works, but of course in real life you would make them transparent and hence invisible! So the user sees just your buttons, evenly distributed on either height of screen.

The reason for the use of this technique is that although the notion of equality performs the distribution of values you are asking for, constraints can apply equality only between aspects of views; thus we need the extra views (the spacer views) so that we have things we can make equal to other things (here, the heights of the spacer views).

Other Approaches

Obviously, a more flexible approach is to assign the constraints in code. This may sound daunting, but there's a lot of third-party code out there to help you, such as this sort of thing.

For example, if we have a (possibly invisible) superview whose height acts as a boundary to dictate maximum vertical distribution of our four buttons, we can pin their tops to the vertical center of that superview with a constant of 0 but a multiplier of 0.000001, 0.666667, 1.33333, and 2.0 respectively (if we have four buttons); now the buttons will stay vertically distributed even as the superview changes size in response to screen height or whatever. [In Xcode 5.1, it will be possible to set that up in Interface Builder, but in earlier versions of Xcode it is not possible.]

这篇关于自动布局中的弹簧:在Xcode 5中均匀分布视图,使用约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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