本文介绍了iOS自动布局:两个等宽的按钮,并排的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在使用 AutoLayout 时遇到困难.我正在使用界面生成器,并尝试将两个等宽的按钮并排放置,如下图所示.

I am currently having difficulty with AutoLayout. I am using interface builder and am trying to position two buttons of equal width side by side as illustrated in the following image.

从下面的预览图像中,我的 titleImage 已得到适当的约束并且可以正确显示,但按钮却没有.我已经尝试将按钮 1 与 titleImage 的前缘对齐,将 button2 与 titleImage 的后缘对齐,但是此时两个按钮之间的宽度分布变得倾斜,如下所示.

From the following Preview image, my titleImage has been properly constrained and is displaying correctly however the buttons are not. I have experimented by aligning button 1 with the leading edge of titleImage, and button2 with the trailing edge of titleImage, however the width distribution between the two buttons becomes skewed at this point, as demonstrated below.

我的目标是了解缺少哪些约束,并应将其应用于两个按钮以保持相同的宽度,而不管设备如何.如果可能的话,我想通过界面生成器而不是附加代码来实现这一点.

My objective is to understand what constraints are missing and should be applied to the two buttons to maintain equal widths regardless of device. If possible I would like to achieve this through interface builder as opposed to additional code.

推荐答案

添加如下约束

  1. 从 button1 到 button2 分配相等的宽度.
  2. 在两个按钮之间分配水平间距.
  3. 将 button1 的前导空格分配给其父视图.
  4. 将 button2 的尾随空格分配给其父视图.
  5. 为两个按钮分配顶部空间.请让我知道这对你有没有用.

这篇关于iOS自动布局:两个等宽的按钮,并排的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 17:06