当前位置: 首页 > 知识库问答 >
问题:

无法使用AutoLayout设置UICollectionViewCell的宽度

薛泰
2023-03-14

我有一个具有动态高度单元格的集合视图(基于可能的多行标签和内部的textview),当我跨越多行时,它在高度上完全适应。但是,当文本只有一个单词或者它没有覆盖整个屏幕宽度时,单元格的宽度正好是所需的宽度,从而导致单元格彼此相邻,而不是在彼此下方。

查看文本视图中具有不同文本长度的结果。

然而,我希望细胞在彼此之下,想想Instagram或Twitter那种视图。显然,我需要为单元格设置一个宽度约束,使其等于屏幕的整个宽度,但我似乎找不到如何做到这一点。

我使用FlowLayout与估计的ItemSize在我的应用委托:

let layout = UICollectionViewFlowLayout()
window?.rootViewController = UINavigationController(rootViewController: HomeController(collectionViewLayout: layout))

layout.estimatedItemSize = CGSize(width: 414, height: 150)

我还为viewDidLoad中的collectionView设置了约束,因此collectionView占据了屏幕的整个宽度和高度:

collectionView?.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
collectionView?.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
collectionView?.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
collectionView?.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

我对标签和文本视图的约束如下所示,还允许宽度采用所需的大小(因此不固定):

// vertical constraints usernameLabel & commentTextView
addConstraintsWithFormat(format: "V:|-16-[v0]-2-[v1]-16-|", views: usernameLabel, commentTextView)

// horizontal constraints usernameLabel
addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: usernameLabel)

// horizontal constraints commentTextView
addConstraintsWithFormat(format: "H:|-16-[v0]-16-|", views: commentTextView)

在我看来,所有这些对于完成这项工作都是至关重要的。然而,在我看来,我还没有设置宽度限制来完成这项工作。我已经尝试在集合视图上添加一个widthAnchor(等于view.widthAnchor),但没有成功:

collectionView?.widthAnchor.constraint(equalTo: self.view.widthAnchor).isActive = true

我还尝试在sizeForItemAt方法中设置宽度,但这也不起作用:

return CGSize(width: view.frame.width, height: 100)

现在,我正在检查宽度以查看它出错的地方,但我得到了我需要的结果,我想:

print("collectionView width: ", collectionView?.frame.size.width)
print("collectionView height: ", collectionView?.frame.size.height)

print("screen width: ", UIScreen.main.bounds.width)
print("screen height: ", UIScreen.main.bounds.height)

结果如下:

collectionView width:  Optional(414.0)
collectionView height:  Optional(736.0)
screen width:  414.0
screen height:  736.0

我不知道如何前进。我看过的所有与这个问题相关的文章或帖子要么明确计算身高,要么使用我上面提到的两种解决方案。

我哪里做错了,有什么想法吗?

共有2个答案

酆君墨
2023-03-14

确保在应用程序中添加此协议

UICollectionViewDelegate
UICollectionViewDataSource
UICollectionViewDelegateFlowLayout

请使用以下方法

    extension YourViewController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {

        func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
             return CGSize(width: screenWidth, height: screenWidth)
        }

    }
谢志用
2023-03-14

更新:

根据Siju Satheesachandran的回答,我为我的HomeController添加了一个扩展:

extension HomeController {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 400, height: 100)
    }
}

然后,在cellForItemAt方法中,我为当前单元格的宽度添加了一个打印:

print("cell width: ", cell.frame.size.width)

返回以下五倍:

cell width:  400.0

所以在我看来,所需的尺寸是可以听取的,但是我的细胞只是适应它想要的任何宽度?

另一个有趣的更新:

当我将TextView的文本更改为太长的文本时,它应该会中断多行,并且我运行该应用程序时,该应用程序会无限次出现此调试错误,而不会加载和显示黑屏:

2018-09-18 15:34:35.806688+0200 prjDynamicCells[39793:1627318] The behavior of the UICollectionViewFlowLayout is not defined because:
2018-09-18 15:34:35.807472+0200 prjDynamicCells[39793:1627318] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2018-09-18 15:34:35.807572+0200 prjDynamicCells[39793:1627318] Please check the values returned by the delegate.
2018-09-18 15:34:35.807911+0200 prjDynamicCells[39793:1627318] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7feb5ec17650>, and it is attached to <UICollectionView: 0x7feb5f841600; frame = (0 0; 414 736); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60000044e6d0>; layer = <CALayer: 0x60000003f4a0>; contentOffset: {0, -64}; contentSize: {414, 1000}; adjustedContentInset: {64, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7feb5ec17650>.
2018-09-18 15:34:35.808009+0200 prjDynamicCells[39793:1627318] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.
 类似资料:
  • 问题内容: 我正在尝试实施一个。当我使用“自动布局”时,单元格不会更改大小,而是对齐。 现在我想将其大小更改为例如 我尝试设置 虽然没有用。 有没有办法做到这一点? 编辑 : 看来,答案只会改变我的CollectionView宽度和高度本身。约束中可能存在冲突吗?有什么想法吗? 问题答案: 使用此方法设置自定义像元高度宽度。 确保添加此协议 如果您使用的是 swift 5 或 xcode 11 及

  • 在过去的三四个小时里,我一直用这个东西把头撞在墙上,我似乎想不出来。我有一个UIViewController,里面有一个全屏的UITableView(屏幕上还有其他东西,这就是为什么我不能使用UITableViewController的原因),我想让我的tableHeaderView用自动布局调整大小。不用说,这并不合作。 见下面截图。 因为overviewLabel(例如“List overvi

  • 问题内容: 我正在尝试使用CSS中的attr()设置元素的宽度,但是它不起作用。Chrome表示“无效的属性值”,但我不确定这是怎么回事。 我正在尝试使用属性“ prog”作为.progress div的百分比宽度。 问题答案: 这是CSS的实验性功能,至少是草案功能,根据Mozilla开发人员网络的文档,当前仅与CSS 属性兼容(在该属性中,它可以返回要放置在伪元素内的字符串),但是不能(尚未)

  • 我希望生成的excel第1列的宽度为。但是excel中的实际宽度是。搜索引擎没有提供合理的解释,谁能帮忙。

  • 我使用ant脚本创建了JDBC连接池。 一切正常,直到我尝试从DB检索数组。我得到以下错误:

  • 本文向大家介绍使用Element UI表格如何设置列宽的宽度自适应?相关面试题,主要包含被问及使用Element UI表格如何设置列宽的宽度自适应?时的应答技巧和注意事项,需要的朋友参考一下 https://kuaizi-co.github.io/kz-table/ 列宽自适应