我有一个场景,我有一个水平滚动的集合视图有许多 X 单元格。单元格的默认宽度为 71 磅。
根据 X 的值,当它们的总宽度小于集合视图的宽度时,我的单元格可能会更少,这很好。如果它们的总宽度(单元格数 * 71)大于 collectionView 的宽度,则显示完全适合的单元格 Y 和下一个单元格的一半,以便用户知道是水平滚动的。
这就是我现在所拥有的
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if dataSource.count * 71 <= collectionView.bounds.width {
return CGSize(width: 71, height: collectionView.bounds.height)
} else {
// if it's larger just show Y number of cells with the last one visible cut in half, so the user knows it's horizontally scrollable
}
}
}