UITableViewCell内的嵌套UIStackViews内的UILabel有时会被截断 [英] UILabel inside nested UIStackViews inside UITableViewCell sometimes truncating

查看:390
本文介绍了UITableViewCell内的嵌套UIStackViews内的UILabel有时会被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a tableview cell in one of my tables setup as containing the following view hierarchy

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

The outer horizontal stackview is pinned to the cell's content view on trailing, leading, bottom and top edges.

正确的标签在后缘,前缘,底部和顶部边缘固定在其父stackViewHackView上

The right label is pinned to its parent stackViewHackView on trailing, leading, bottom and top edges

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

Within my controllers code I've got the cells setup to be dynamic height to fit the content of the cells so they can grow or shrink depending on the content size

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 30.0 

关于自动版面设置. ( BOLD 中的相关优先级)

Regarding Auto layout setup. (Relevant priorities in BOLD)

  • 外部水平堆栈视图
    • 内容拥抱优先级== 250
    • 内容抗压缩优先级== 750
    • 内部垂直堆栈视图
      • 内容拥抱优先级== 750
      • 内容抗压缩优先级== 750
      • 左标签1
        • 内容拥抱优先级== 750
        • 内容抗压缩优先级== 750
        • Outer Horizontal Stack View
          • Content Hugging Priority == 250
          • Content Compression Resistance Priority == 750
          • Inner Vertical Stack View
            • Content Hugging Priority == 750
            • Content Compression Resistance Priority == 750
            • Left Label 1
              • Content Hugging Priority == 750
              • Content Compression Resistance Priority == 750
              • 内容拥抱优先级== 750
              • 内容抗压缩优先级== 750
              • 内容拥抱优先级== 250
              • 内容抗压缩优先级== h:750; v:250
              • Content Hugging Priority == 250
              • Content Compression Resistance Priority == h:750; v:250
              • 内容拥抱优先级== 250
              • 内容抗压缩优先级== h:250; v:750
                • 降低水平阻力,以允许此视图压缩以适合左侧堆栈视图
                • Content Hugging Priority == 250
                • Content Compression Resistance Priority == h:250; v:750
                  • lower horizontal resistance to allow this view to compress for the left stack view to fit
                  • 内容拥抱优先级== 250
                  • 内容抗压缩优先级== h:250; v:750
                    • 降低水平阻力,以允许此视图压缩以适合左侧堆栈视图
                    • Content Hugging Priority == 250
                    • Content Compression Resistance Priority == h:250; v:750
                      • lower horizontal resistance to allow this view to compress for the left stack view to fit

                      我在带有以下代码的简单测试应用程序中用测试数据填充此单元格

                      I fill this cell with test data in a simple test app with the following code

                      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目标上的模拟器中产生以下渲染结果

                      Which results in the following rendering in the simulator on an iPhone 7 target

                      中间的tableview单元格是我一直想要得到的输出.我不希望左侧标签被截断,它们的内容将始终足够小以适合右侧.然而,鉴于右侧的某些内容,较长的Label 2有时会被截断,就像您在顶部和底部的行中所看到的那样.

                      The middle tableview cell is the sort of output i'd like to get all the time. I don't want the left labels to EVER truncate, their content will always be small enough to fit along with the right hand side. Yet given certain content on the right hand side the longer Label 2 can sometimes be truncated as you can see in the top and bottom rows.

                      我已经设置了内容压缩抗性,以使左侧的所有元素都具有较高的抗压缩性,而右侧的元素被设置为较低的抗性,以便它们可以压缩以为左侧元素留出空间.

                      I've setup the content compression resistance such that all the elements on the left have high resistance to compression with the elements on the right set to lower resistance so that they should compress to make space for the left side elements.

                      有什么想法为什么会发生这种情况或如何阻止这种情况发生,以便我的标签始终正确显示?

                      Any ideas why this is happening or how I can stop it happening so my labels always display properly?

                      干杯!

                      推荐答案

                      (根据操作规范添加为答案)

                      这可能只是在我看来",但是...我已经看到许多人在具有约束的简单视图可以完成工作的同时,使用包裹在包裹在包裹视图中的包裹视图.

                      This may just be "how it seems to me" but... I've seen a number of people using stack views wrapped in stack views wrapped in stack views, when simple views with constraints can do the job.

                      看看我在这个项目中是如何做到的:github.com/DonMag/CellTest2

                      Take a look at how I've done it in this project: github.com/DonMag/CellTest2

                      这真的非常简单,您可以查看标签,看看在更改优先级方面我要做的事很少.我使用了示例字符串,并添加了更多案例来测试变体.

                      It's really very simple, and you can look at the labels and see just how little I had to do in terms of changing priorities. I used your sample strings, and added a few more cases to test variations.

                      这篇关于UITableViewCell内的嵌套UIStackViews内的UILabel有时会被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆