无法使用 AutoLayout 设置 UICollectionViewCell 的宽度

2024-02-17

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

看看文本视图中不同长度文本的结果。 https://i.stack.imgur.com/mD2sn.jpg

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

我在 AppDelegate 中将 FlowLayout 与estimatedItemSize 一起使用:

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)

在我看来,所有这些对于这项工作的顺利进行至关重要。然而,在我看来,我还没有设置宽度约束来完成这项工作。我尝试在 collectionView 上添加 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

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

关于我做错了什么有什么想法吗?


Update:

我根据 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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法使用 AutoLayout 设置 UICollectionViewCell 的宽度 的相关文章