如何快速为包含在stackview中的标签赋予动态高度

时间:2018-08-17 09:26:19

标签: ios swift uitableview uistackview

在UItableview单元格内部,我具有以下结构。

enter image description here

它具有左右两个Stackviews
1)Left Stackview包含3个视图。每个视图包含1个标签(Label1,Label2,Label3)
2)Right Stackview包含2个视图。每个视图包含1个标签(Label5,Label6)

相对于其父视图,Label1,Label2和Label3的约束为Top,Left,Right和Bottom。

现在,当我在运行时将文本添加到Label1,Label2和Label3时,它具有动态高度,但在顶部和底部留有空间,对于那些文本较少的标签。下面是输出。

enter image description here

我想减少所有三个标签的顶部和底部空间,即Label1,Label2,Label3。

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:0)

  

这可能对您有帮助

我认为您已经使用了堆栈视图的分布作为填充,因此它为每个视图提供了相等的空间。     我认为为此,您必须按比例使用填充。这将相应地调整高度

Choose fill proportionally from here

if this not working then choose fill proportionally and give height to each label it will adjust.

检查如何赋予标签高度。 enter image description here

像这样在堆栈视图中指定每个标签的高度后,

。 您将根据数据进行自我调整。

enter image description here

答案 1 :(得分:0)

对于动态高度,请在表格视图中使用自定义尺寸的单元格。为tableView设置rowHeight和estimateHeight属性

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100

将stackview分发属性设置为.fillproportionally

根据.fillproportionally属性的UIStackView documentation

  

一种布局,其中堆栈视图调整其排列的视图的大小,以便它们   沿堆栈视图的轴填充可用空间。视图是   根据其内在内容的大小按比例调整大小   堆栈视图的轴。

代码中:

stackView.distribution = fillproportionally

或在情节提要中

enter image description here

答案 2 :(得分:0)

请检查所附的屏幕截图,其中有完整的分步指南。如何使用多个UILabel在垂直StackView中动态设置UILabel的高度。

如有任何疑问,请发表评论。

Check the attached screenshot

相关问题