当给予相等宽度约束按钮时,自动布局未正确设置 [英] Auto layout not set properly when give equal width constrains to buttons

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

问题描述

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



当前结果:





给予所有按钮的约束:





查看层次结构:



解决方案

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



您无需在视图中添加它们。只需按照以下步骤操作:



<让我们从这开始:





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



橙色按钮添加约束:



添加以下约束:
行距,底部空间和相等高度。





blue 按钮添加约束:



添加以下约束:
尾随,底部空间和相等高度。





青色按钮添加约束:




  • 按住Ctrl +从青色按钮拖动到橙色按钮并添加以下约束:水平宽度ng和垂直居中。






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






  • 底部空间和相等的高度。





到目前为止,我们差不多完成了!



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





您的按钮应如下所示:





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







你去了:





我希望这有所帮助,干杯。


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

Current Result:

Constrains given to all button:

View Hierarchy:

解决方案

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:

let's start with this:

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:

add the following constraints: leading, bottom space, and equal height.

Adding constraints to the blue button:

add the following constraints: trailing, bottom space and equal height.

Adding constraints to the cyan button:

  • ctrl + drag from the cyan button to the orange button and add the following constraints: horizontal spacing and center vertically.

  • ctrl + drag from the cyan button to the blue button and add the following constraints: horizontal spacing.

  • bottom space and equal height.

So far so good, we are almost done!

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

Your buttons should look like this:

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

AND there you go:

I hope this helped, Cheers Up.

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

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