首页 > 解决方案 > 使用 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)
}

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

标签: iosswiftuicollectionviewuicollectionviewdelegateflowlayout

解决方案


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

viewWillLayoutSubviews()

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

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

或者

layoutSubviews()

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


推荐阅读