自动调整嵌入在UITableView中的UICollectionView单元的大小不起作用 [英] Autosizing UICollectionView cell embedded in UITableView not working

查看:243
本文介绍了自动调整嵌入在UITableView中的UICollectionView单元的大小不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经花了几天的时间尝试各种SO帖子上建议的内容,但无法弄清楚.我可能正在看这个问题!

I have spent a few day's now trying out things suggested on various SO posts but have not been able to figure this out. I'm probably looking right at the issue!

我正在从Android切换到iOS,似乎在自动布局方面遇到了一些麻烦.具体而言,使UICollectionView调整为其内容的高度.每个集合视图根据其内容可以具有不同的单元格大小,但是给定行中每个项目的单元格大小都相同.

I am making a switch over from Android to iOS and seem to be having some trouble with autolayout. Specifically, getting the UICollectionView to resize to the height of its content. Each collection view can have a different cell size based on its content but the cell size will be the same for each item on a given row.

UICollectionView嵌套在UITableViewCell中. UITableViewCells似乎正在正确调整其内容高度的大小,并在Debug View Hierarrchy中查看问题似乎是UICollectionView.

The UICollectionView is nested inside a UITableViewCell. The UITableViewCells appear to be resizing to its content height correctly and looking at the Debug View Hierarrchy the issue seems to be the UICollectionView.

我创建了一个迷你演示并将其放在此github(下面的链接)上.我已经抛出了很多测试数据,因此ViewController VC的底部有些混乱.该演示仅说明了每一行如何具有不同的内容大小.可以有几张样式相同的表行.

I have created a mini demo and put it on this github (link below). I have thrown in a bunch of test data so the bottom part of the ViewController VC is a bit messy. The demo just illistrates how each row can have different content size. There can be several table rows of the same style.

注意:我必须将以下行添加到CategoryRowCell.swift中,以便获得以下屏幕截图.没有此行,UICollectionView高度将变为0!

NOTE: I had to add the following line to CategoryRowCell.swift in order for me to get the below screen shots. Without this line the UICollectionView height becomes 0!

...
collectionView.heightAnchor.constraint(equalToConstant: 250)
...

GitHub链接

这就是现在的样子.

向下滚动

请注意水平列表顶部和底部的间隙.这就是我所期望的..

Notice the gaps at the top and bottom of the horizontal lists. Here is what i am expecting..

这是调试视图层次结构的一部分.您将看到该收集视图已经具有顶部和底部页边距(良好),但是红线显示了应用于该收集视图的多余高度.绿线表示我期望的高度.我希望收藏视图具有其内容的高度.

Here is part of the debug view hierarchy. You will see that the collection view already has top and bottom margins (good), but the red lines show the excess height being applied to the collection view. The green line indicates the height i am expecting. I am wanted the collection view to have a height of its content.

推荐答案

我认为最好采用另一种方法,将UICollectionView作为父级,将UITableView作为子级.因为第一个更加灵活和可定制.

I think it would be better to do it the other way around, having the UICollectionView as the parent and UITableView as the child. Since the first is more flexible and customizable.

无论如何,我认为您需要看两件事:

Anyway for your case I think you need to take a look at two things:

1-要更好地控制单元格大小,可以将此函数用于UICollectionView:

1- To have a better control of the cell size you can use this func for UICollectionView:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize { 

  // The desired size based on the value at that row (the name for example)

  if objects[indexPath.row].name == "some condition" {
    return CGSize(width: 150, height: 50)
  }
  else {
    return CGSize(width: UIScreen.main.bounds.width, height: 75)
  }


}

2- UICollectionView和UITableView的大小检查器值(您可能在其中之一中有一些额外的间距)

2- The size inspector values for the UICollectionView and UITableView (you might have some extra spacing in one of them)

这篇关于自动调整嵌入在UITableView中的UICollectionView单元的大小不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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