简体   繁体   English

如何在 StackView 中设置等宽或等高?

[英]How to Set Equal Width or Equal Height in StackView?

I have six labels here, every two labels are stack in a horizontal view, and all horizontal views are stack in a vertical view, as show below我这里有六个标签,每两个标签在水平视图中堆叠,所有水平视图在垂直视图中堆叠,如下图

标签布局细节

直观地标记布局细节

Here is what I want: I'd like to set Label1 's width 30% of Label2 regardless of each label's content这是我想要的:无论每个标签的内容如何,​​我都想将Label1的宽度设置为Label2 30%

The outer stack view is set as Aligment:Fill, Distribution:Fill Equally外部堆栈视图设置为 Aligment:Fill, Distribution:Fill Equally

All inner stack view are set as Aligment:Fill, Distribution:Fill所有内部堆栈视图都设置为对齐:填充,分布:填充

I have set Label1 's hugging to 250, and Label2 's as default (so does the others).我已将Label1的拥抱设置为 250,并将Label2为默认值(其他人也是如此)。

And I am trying to set the width by Equal Widths in pic 2. But it is unselected我试图在 pic 2 中通过 Equal Widths 设置宽度。但它没有被选中

what should I do about this?我该怎么办? Preferring doing all the work in the storyboard, but working around in code is ok too.更喜欢在故事板中完成所有工作,但在代码中工作也可以。


Solution: Just need to select both label to add the constraint, as Below Solution image解决方案:只需要选择两个标签来添加约束,如下图解决方案

IMO, Stack View wont allow to give some fix frame size of the views inside it, we can somewhat control it using available properties. IMO,Stack View 不允许为其中的视图提供一些固定的框架大小,我们可以使用可用属性在一定程度上控制它。
Stack Views are meant for similar type content to be confined inside one view.堆栈视图旨在将类似类型的内容限制在一个视图中。

Option:选项:

  • Take your Labels inside one View (Content View).将您的标签放入一个视图(内容视图)中。
  • Set the width of Right label equal to the Left Label , set the multiplier to 0.30 .Right label的宽度设置为Left Label ,将乘数设置为0.30

故事板

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

相关问题 如何将视图高度设置为等于其宽度 - How to set a view height to be equal to its width Xcode如何在水平堆栈视图中创建方形按钮(宽度等于高度) - Xcode How to create a square button(width equal to height) inside a Horizontal stackview 设置UIImage的高度等于其宽度 - Set UIImage's height equal to its width 使用等宽标签为水平 StackView 添加分隔符视图 - Adding Separator View for Horizontal StackView with Equal Width Labels 如何将uilabel的高度设置为等于uitableviewcell - how to set height of uilabel equal to uitableviewcell 像元高度等于图像宽度 - Cell height equal to image width 如何在设置了“等宽”的scrollview中嵌入的stackview中,将标签文本设置为远离视图边缘? - How to set label text away from edge of view in a stackview embedded in scrollview where 'equal widths' is set? 如何使用Swift中的SnapKit制作宽度和高度相等的UIView? - how to make UIView with equal width and height with SnapKit in Swift? 如何设置自动布局约束,以使UIImageView的高度等于超级视图的高度? - How to set autolayout constraint so that the height of an UIImageView is equal to the height of the superview? iOS - 如何将视图宽度设置为等于TableView分隔符宽度 - iOS - How to set View width equal to TableView Separator width
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM