首页 > 解决方案 > UICollectionViewCell 大小错误

问题描述

我有一个问题UICollectionViewCell

UICollectionViewCell的宽度 =UIScreen.main.bounds.size.width在我添加UILabel到具有左右边缘约束的单元格之前它工作正常。如果我只添加左边它没关系。但是当我有一个问题时。

错误状态:

漏洞 在此处输入图像描述

正常状态:

在此处输入图像描述 在此处输入图像描述

标签: iosobjective-cswiftlayoutstoryboard

解决方案


extension YourViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: 50) 

}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
    return .zero
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}
}

你可以试试这段代码。在情节提要 -> UICollectionView -> 在大小检查器中将估计大小更改为无


推荐阅读