ios - UICollectionViewCell 大小错误
问题描述
我有一个问题UICollectionViewCell
。
我UICollectionViewCell
的宽度 =UIScreen.main.bounds.size.width
在我添加UILabel
到具有左右边缘约束的单元格之前它工作正常。如果我只添加左边它没关系。但是当我有一个问题时。
错误状态:
正常状态:
解决方案
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 -> 在大小检查器中将估计大小更改为无