为按钮提供相等的宽度约束时,自动布局设置不正确 [英] Auto layout not set properly when give equal width constrains to buttons

查看:17
本文介绍了为按钮提供相等的宽度约束时,自动布局设置不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了许多解决方案并应用了自动布局约束,但我仍然没有得到想要的解决方案.

I see many solution and applied auto layout constrains but still i am not getting desired solution.

当前结果:

给所有按钮的约束:

查看层次结构:

推荐答案

假设您想要的是让 3 个按钮彼此相等并使用 Interface Builder 填充屏幕宽度(没有代码,即:不是以编程方式),这是一个解决方案:

Assuming that what do you want is letting the 3 buttons equal each others and filling the width of screen using the Interface Builder (with no code, i.e: not programmatically), this is a solution:

您不需要将它们添加到视图中.只需按照以下步骤操作:

you don't need to add them in views. just follow these steps:

让我们从这个开始:

请注意,按钮还没有任何约束,只需将它们添加到视图底部(或者您想要显示它们的任何位置,对于此解决方案,我将它们显示在底部屏幕).确保它们中的 3 个具有相同的大小.

note that the buttons don't have any constraints -yet-, just add them to the to the bottom of the view (or where ever you want to display them, for this solution, I will display them in bottom of the screen). Make sure that the 3 of them have the same size.

橙色按钮添加约束:

Adding constraints to the orange button:

添加以下约束:前导、底部空间和等高.

蓝色按钮添加约束:

Adding constraints to the blue button:

添加以下约束:尾随、底部空间和等高.

青色按钮添加约束:

Adding constraints to the cyan button:

  • ctrl + 从青色按钮拖动到橙色按钮并添加以下约束:水平间距和垂直居中.

  • ctrl + 从青色按钮拖动到蓝色按钮并添加以下约束:水平间距.

  • 底部空间和等高.

到目前为止一切顺利,我们几乎完成了!

So far so good, we are almost done!

现在,选择 3 个按钮并添加以下约束:等宽.

Now, select the 3 buttons and add the following constraint: equal widths.

您的按钮应如下所示:

现在,您所要做的就是选择两个水平间距约束中的每一个,并将它们的常量设置为 0(来自尺寸检查器):

Now, all you have to do is select each of the two horizontal spacing constraints and set their constants to 0 -from the size inspector-:

你去吧:

我希望这会有所帮助,干杯.

I hope this helped, Cheers Up.

这篇关于为按钮提供相等的宽度约束时,自动布局设置不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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