Как выровнять ячейки по центру в UICollectionView - Swift 5
func collectionView(_ collectionView: UICollectionView, layout
collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
guard let flowLayout = collectionViewLayout
as? UICollectionViewFlowLayout
else {
return .zero
}
let cellCount = CGFloat(
collectionView.
numberOfItems(inSection: section))
if cellCount > 0 {
let cellWidth = flowLayout.
itemSize.width + flowLayout.
minimumInteritemSpacing
let totalCellWidth = cellWidth * cellCount
let contentWidth = collectionView.
frame.
size.
width - collectionView.
contentInset.
left - collectionView.
contentInset.
right - flowLayout.headerReferenceSize.
width - flowLayout.
footerReferenceSize.
width
if (totalCellWidth < contentWidth) {
let padding = (contentWidth - totalCellWidth + flowLayout.minimumInteritemSpacing) / 2.0
return UIEdgeInsets(
top: 0,
left: padding,
bottom: 0,
right: 0)
}
}
return .zero
}
Возврат к списку