【问题标题】:UICollectionViewCell wrong sizeUICollectionViewCell 大小错误
【发布时间】:2020-03-22 08:19:51
【问题描述】:
UICollectionViewCell 有问题。
我的UICollectionViewCell 的宽度 = UIScreen.main.bounds.size.width,在我将 UILabel 添加到具有左右边缘约束的单元格之前,它工作正常。如果我只添加左边它没关系。但是什么时候我都有问题。
错误状态:
正常状态:
【问题讨论】:
标签:
ios
objective-c
swift
layout
storyboard
【解决方案1】:
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
}
}
你可以试试这个代码。
在 storyboard -> UICollectionView -> 在 Size Inspector 中将 Estimate size 更改为 None
【解决方案2】:
你可以设置大于等于约束的右约束,这样可以适当调整。
【解决方案3】:
不要向 uicollection 单元格添加右约束。
只需提供顶部和左侧(前导)约束并将 uilabel 拖动到所需的宽度,然后设置宽度约束。
这应该是您获得所需结果所需的全部内容。