简体   繁体   English

Xcode如何在水平堆栈视图中创建方形按钮(宽度等于高度)

[英]Xcode How to create a square button(width equal to height) inside a Horizontal stackview

I have a requirement to create 3 square buttons at bottom of the page. 我需要在页面底部创建3个方形按钮。 All of them must be aligned and their size should be square. 所有这些都必须对齐,尺寸应该是正方形。

So far, I have created a horizontal stackview and created autolayout constrains 50 from the left, 50 from the right, 10 from the bottom and set the stackview height as 60. 到目前为止,我已经创建了一个水平堆栈视图,并创建了autolayout约束50从左边,50从右边,10从底部约束,并将stackview高度设置为60。

How can I create square buttons, since the stackview subview width is dynamic and determined at run time? 如何创建方形按钮,因为stackview子视图宽度是动态的并且在运行时确定?

How can I set an equal aspect ratio for these three buttons? 如何为这三个按钮设置相等的宽高比?

由于高度不合适导致的圆形图像问题

Don't set the stack view height; 不要设置堆栈视图高度; let the content determine that. 让内容确定。

You haven't said exactly how you want the buttons laid out, but I assume you want them equally distributed. 你还没有准确地说出你想要按钮的布局,但我认为你希望它们平均分配。

  1. Put the buttons in a horizontal stack view 将按钮放在水平堆栈视图中
  2. Constrain the leading, trailing and bottom edges of the stack view to the superview 将堆栈视图的前沿,后沿和下边缘约束到superview
  3. Set the stack view's alignment to "Fill" and distribution to "equal spacing" 将堆栈视图的对齐设置为“填充”并将分布设置为“等间距”
  4. On each button, create an aspect ratio constraint between its width and its height of 1:1 在每个按钮上,在宽度和高度1:1之间创建宽高比约束
  5. If you want a minimum height for the buttons, set a >= height constraint on one of them 如果您想要按钮的最小高度,请在其中一个上设置> = height约束
  6. If you want a fixed height for the buttons, set a = height constraint on one of them 如果要为按钮设置固定高度,请在其中一个上设置a = height约束

I think the only thing you are missing is setting the Distribution in Attributes Inspector on your Stack View. 我认为你唯一缺少的是在Stack View上设置Attributes Inspector中的Distribution。 Set it to distribute equally as shown in the image below: 将其设置为均匀分布,如下图所示:

在此输入图像描述

To set the height, you simply need to set a height contraint on each button. 要设置高度,您只需在每个按钮上设置高度约束。 Click the tie fighter looking icon and set the height for each: 单击领带战斗机外观图标并为每个设置高度:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM