Xcode自动布局-约束等于另一个约束 [英] Xcode Autolayout - Constraint equal to another constraint

查看:188
本文介绍了Xcode自动布局-约束等于另一个约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于试图在IB中采用自动布局,但是在设置某些对象的约束时遇到了麻烦.我基本上希望5个视图在整个Superview中垂直分布.我有3行按钮,每行2行.我希望间距D1,D2,D3和D4等于没有来调整任何东西的高度.如何在IB中设置?

I am finally trying to adopt autolayout in IB and I am having trouble setting the constraints for some objects. I basically want 5 views to be equally distributed vertically throughout the superview. I have 3 buttons separated by 2 lines. I want the spacings D1, D2, D3, and D4 to be equal WITHOUT resizing the height of anything. How can this be set up in IB?

推荐答案

您可以为D1,D2,D3和D4创建UILayoutGuide对象(或者,对于iOS 9之前的iOS版本,UIView对象). ,spacer1spacer2等,然后将它们的高度定义为等于每个高度.在视觉格式语言(VFL)中,垂直约束类似于:

You can create UILayoutGuide objects (or, for iOS versions before iOS 9, UIView objects) for D1, D2, D3, and D4, e.g., spacer1, spacer2, etc., and then define their heights to be equal to each over. In Visual Format Language (VFL), the vertical constraint would look something like:

@"V:|-[button1][spacer1][separator1(4)][spacer2(==spacer1)][button2][spacer3(==spacer1)][separator2(4)][spacer4(==spacer1)][button3]-|"

我不知道您如何表示分隔线(这里我只是使用4点高的UIView,但是您可以按照自己的方式进行操作).但是希望这可以说明这个想法.

I don't know how you're representing your separator lines (here I'm just using a 4 point tall UIView, but you can do it any way you want). But hopefully this illustrates the idea.

如果在Interface Builder中进行此操作,则可以在可见控件之间添加UIView对象,然后选择所有间隔视图(例如,通过按住Shift键单击),然后添加约束以固定高度"同样",使它们的高度相同.

If you were doing this in Interface Builder, you can add UIView objects in between the visible controls, and then select all of the spacer views (e.g. by shift-clicking) and then add a constraint to pin the "Heights Equally" to make them all the same height.

通过在每个按钮和分隔线的NSLayoutAttributeCenterY属性上创建约束,并为每个按钮调整multiplier值以使它们均匀分布,可以在没有分隔符视图的情况下以编程方式实现相似的效果.效果非常相似,但不完全相同.

You can achieve a similar effect programmatically without the spacer views by creating a constraint on the NSLayoutAttributeCenterY attribute of each button and separator line and adjusting the multiplier value for each such that they're evenly spaced. The effect is very similar, but not identical.

这篇关于Xcode自动布局-约束等于另一个约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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