What is the best approach for horizontally aligning 5 buttons with Autolayout(使用自动布局水平对齐 5 个按钮的最佳方法是什么)
问题描述
我知道这是一个新手风格的小问题,但我找不到任何主题,我必须在视图控制器中水平对齐多个按钮.
I know this is a little newbie style question but I couldn't find any subject about this and I have to horizontally align multiple buttons in my view controller.
我尝试了 2 种方法,第一种方法是将它们与 UIViews 和 <= 宽度更改约束优先级等分开.第二个没有 UIViews 但按钮在右舷对齐得很好.我已经看过斯坦福汽车布局课程.但我找不到任何解决方案.我不知道如何对齐多个按钮或视图,有人可以帮助我学习和解决这个问题吗?
I tried to 2 approach first one is separate them with UIViews and <= width changed constraints priority and others. Second one doesn't have UIViews but buttons aligned in starboard very well. I already watched Stanford auto layout lessons. But I couldn't find any solution about this. I don't know how to approach to align multiple buttons or views can someone help me to learn and solve this problem ?
谢谢!
推荐答案
你只需要在相邻按钮之间给出前导、尾随和等宽约束.
You just need to give leading, trailing and equal-widths constraints between adjacent buttons.
步骤(这里我们只考虑水平约束):
- 将第一个按钮的前导约束添加到父视图(或左侧同级视图).
- 在以下之间添加水平间距约束:
- 第一个按钮和第二个按钮.
- 第二个按钮和第三个按钮.
- 第三个按钮和第四个按钮.
- 第四个按钮和第五个按钮.
- 第一个按钮和第二个按钮.
- 第二个按钮和第三个按钮.
- 第三个按钮和第四个按钮.
- 第四个按钮和第五个按钮.
如果您采用这种方法,则无需添加额外的视图来对齐按钮.请查看下面的屏幕截图以更好地理解这一点.
If you follow this approach, you need not add extra views to align the buttons. Please see the screenshots below for understanding this better.
这篇关于使用自动布局水平对齐 5 个按钮的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用自动布局水平对齐 5 个按钮的最佳方法是什么
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
