如何将两个按钮与UIView的中心等距离对齐 [英] How to align two buttons equidistance from the centre of the UIView

查看:149
本文介绍了如何将两个按钮与UIView的中心等距离对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自动布局。面对有趣的问题。幸运的是当时克服它,但需要知道解决问题的最佳方法。

I am working with auto layout . Facing on interesting issue . Fortunately overcame from it for the time but need to know the best way to solve it .

问题:
如上图所示,我有一对按钮。两者都需要放置在距离UIView中心等距离的位置(粗体垂直线是视图的中心)。此外,我必须以这样的方式应用约束,即按钮之间的距离将自动调整。例如距离应为设备宽度的25%。如果我的设备宽度为320,则按钮之间的距离为80像素,依此类推。

Problem : As showing in the above figure I have couple of buttons . Both needed to place at equidistance width from the centre of the UIView (bold vertical line is centre of the view). Also I have to apply to constraint in such a way that distance between the button will be automatically adjusted. for e.g. the distance should be 25% of the device width. If my device width is 320 the distance between the button is 80 pixels and so on.

解决方案(我已经尝试过):
对于上述问题,我尝试了解决方案。如图所示,我在UIView中添加了虚拟视图,虚拟视图的宽度等于两个按钮之间的间距。然后我将约束应用于虚拟视图。例如: 1。 UIView 2.Width的水平中心等于UIView宽度,0.25为乘数

问题:
以上解决方案对我来说非常合适。但是如果将来如果我的视图会有太多复杂的元素,那么添加虚拟视图可能不是一个好主意。那么,有没有其他干净的方法呢?

Question: above solution is working perfectly for me . But if in future if my view will have too many complex elements then adding dummy view might not be the good idea . So, Is there any other clean way to do this ?

推荐答案

我遇到了同样的问题。我解决了我从rdelmar的回答中得到的想法。

I faced the same problem. I resolved using the idea i got from rdelmar's answer.

这就是我做的。

左键:


  1. 将约束添加到'中心 - 水平容器'

  2. 然后将乘数更改为对于该约束,0.5而不是1。

对于右键:


  1. 将约束添加到'中心 - 水平容器'

  2. 然后将乘数更改为1.5而不是1约束。

让我知道它是否适合你。

Let me know if it worked for you.

这篇关于如何将两个按钮与UIView的中心等距离对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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