无法使用AutoLayout设置UICollectionViewCell的宽度

时间:2018-09-18 09:54:18

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewflowlayout

我有一个具有动态高度单元格的Collection视图(基于可能的多行标签和内部的textview),当我将其跨多行时,它可以完美地适应高度。但是,当文本仅是一个单词或不能覆盖整个屏幕宽度时,该单元格就等于需要的宽度,从而导致单元格彼此相邻而不是彼此下方。

Take a look at the result with different lengths of text inside the textview.

但是,我希望这些单元格位于彼此之间,以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

我对标签和textview的约束看起来像这样,也允许宽度采用所需的大小(因此它不是固定的):

// 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

我对如何前进没有想法。我所涉及的所有文章或帖子都明确计算了高度,或者使用了我上面提到的两种解决方案。

关于我在做什么错的任何想法吗?

2 个答案:

答案 0 :(得分:0)

确保在应用中添加此协议

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)
        }

    }

答案 1 :(得分:0)

更新

根据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.