UITableViewCell内部的UIStackViews内部的UILabel有时会被截断

我有一个tableview单元格在我的一个表中设置为包含以下视图层次结构

TableView单元格视图层次结构

外部水平堆栈视图固定在单元格的内容视图的尾部,前部,底部和顶部边缘。

正确的标签固定在它的父级stackViewHackView尾部,前部,底部和顶部边缘

在我的控制器代码中,我已经将单元格设置为dynamic高度以适应单元格的内容,以便它们可以根据内容大小增长或缩小

tableView.rowHeight = UITableViewAutomaticDimension tableView.estimatedRowHeight = 30.0 

关于自动布局设置。 ( BOLD的相关优先事项)

  • 外部水平堆栈视图
    • 拥抱优先级的内容== 250
    • 内容压缩抵抗优先级== 750
    • 内部垂直堆栈视图
      • 内容拥抱优先== 750
      • 内容压缩抵抗优先级== 750
      • 左标签1
        • 内容拥抱优先== 750
        • 内容压缩抵抗优先级== 750
      • 左标签2
        • 内容拥抱优先== 750
        • 内容压缩抵抗优先级== 750
      • 查看(只需将剩下的两个标签粘贴到堆栈视图的顶部)
        • 拥抱优先级的内容== 250
        • 内容压缩抵抗优先== h:750; 五:250
    • stackViewHackView(hack使多行标签在堆栈视图和单元格内正常生长)
      • 拥抱优先级的内容== 250
      • 内容压缩抵抗优先== h:250; 五:750
        • 降低水平阻力以允许该视图压缩左堆视图以适合
    • 正确的标签
      • 拥抱优先级的内容== 250
      • 内容压缩抵抗优先== h:250; 五:750
        • 降低水平阻力以允许该视图压缩左堆视图以适合

我用一个简单的testing应用程序用下面的代码填充这个单元格的testing数据

 override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "testCell", for: indexPath) as! TestTableViewCell // Configure the cell... if indexPath.section == 0 { cell.leftLabel1?.text = "Label1 aabbccdd" cell.leftLabel2?.text = "Label2 aabbccdd" cell.rightLabel?.text = "Cell \(indexPath.row) with a 'really really' long string that needs displaying clearly and causing the table cell to resize to fit the content. We're looking for about 3 or 4 lines of text to be displayed" } else if indexPath.section == 1 { cell.leftLabel1?.text = "Label1 aabbccdd" cell.leftLabel2?.text = "Label2 aabbccdd" cell.rightLabel?.text = "Cell \(indexPath.row) with a 'really really really' long string that needs displaying clearly and causing the table cell to resize to fit the content. We're looking for about 3 or 4 lines of text to be displayed" } else { cell.leftLabel1?.text = "Label1 aabbccdd" cell.leftLabel2?.text = "Label2 aabbccdd" cell.rightLabel?.text = "Cell \(indexPath.row) with a 'really really really really really really really really' long string that needs displaying clearly and causing the table cell to resize to fit the content. We're looking for about 3 or 4 lines of text to be displayed" } return cell } 

其结果是在iPhone 7目标上的模拟器中进行以下渲染

模拟器截图

中间的tableview单元格是我想要一直得到的那种输出。 我不希望左边的标签被截断,它们的内容总是足够小,以适应右边。 然而,如果在右侧给出了某些内容,那么较长的标签2有时会被截断,就像在顶部和底部行中所看到的一样。

我已经设置了内容压缩电阻,使得左侧的所有元件都具有较高的抗压缩性,右侧的元件设置为较低的电阻,使得它们应该压缩以为左侧元件留出空间。

任何想法,为什么发生这种情况,或者我怎么能阻止它发生,所以我的标签总是正确显示?

干杯!

(作为每个OP的答案添加)

这可能只是“在我看来如何”,但是…我已经看到了许多使用堆栈视图封装在堆栈视图中的堆栈视图,当有约束的简单视图可以完成这项工作时。

看看我在这个项目中做了些什么:github.com/DonMag/CellTest2

这真的很简单,你可以看看标签,看看我在改变优先事项方面做得多less。 我用你的示例string,并添加了几个案例来testing变化。