如何使用 UIKit 中的 UICollectionView 实现这样的布局?我需要添加一个新单元格,其高度大于正常单元格的高度。 AFAIK UICollectionView 一行中的所有单元格必须具有相同的高度。
您可以通过以下方式设置特定的单元格大小:
extension YourViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item == /*your desired cell*/ {
return CGSize(width: doubleWidth, height: doubleHeight)
} else {
return CGSize(width: normalWidth, height: normalHeight)
}
}
}