使用自动布局水平对齐 5 个按钮的最佳方法是什么

2023-09-09移动开发问题
6

本文介绍了使用自动布局水平对齐 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.

步骤(这里我们只考虑水平约束):

  1. 将第一个按钮的前导约束添加到父视图(或左侧同级视图).
  2. 在以下之间添加水平间距约束:
    • 第一个按钮和第二个按钮.
    • 第二个按钮和第三个按钮.
    • 第三个按钮和第四个按钮.
    • 第四个按钮和第五个按钮.
  • 第一个按钮和第二个按钮.
  • 第二个按钮和第三个按钮.
  • 第三个按钮和第四个按钮.
  • 第四个按钮和第五个按钮.

如果您采用这种方法,则无需添加额外的视图来对齐按钮.请查看下面的屏幕截图以更好地理解这一点.

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 个按钮的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

[ios.cocos2d+box2d]如何禁用自动旋转?
[ios.cocos2d+box2d]how to disable auto-rotation?([ios.cocos2d+box2d]如何禁用自动旋转?)...
2024-08-12 移动开发问题
7

cocos2D iPhone中的UItextfield文本对齐问题
UItextfield text alignment issue in cocos2D iPhone(cocos2D iPhone中的UItextfield文本对齐问题)...
2024-08-12 移动开发问题
7

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9