使用UICollectionViewDelegateFlowLayout后约束未更新

时间:2019-02-14 08:44:06

标签: ios swift uicollectionview uicollectionviewdelegateflowlayout

我正在使用UICollectionViewDelegateFlowLayout来设置collectionViewCell的大小和高度,但是当我使用它时,cell中的约束不会更新。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: UIScreen.main.bounds.width - 30, height: self.height)
}

因此,委托方法设置了正确的大小,但是单元格中的元素被剪切(不更新)。我的单元格约束在情节提要中设置。

1 个答案:

答案 0 :(得分:1)

没有太多细节,我想您可能需要:

viewWillLayoutSubviews()

  

被调用以通知视图控制器其视图将要布局其子视图。

https://developer.apple.com/documentation/uikit/uiviewcontroller/1621437-viewwilllayoutsubviews

layoutSubviews()

https://developer.apple.com/documentation/uikit/uiview/1622482-layoutsubviews