I am now making a collectionView for pics and I wanna make it like below.
However, when I scroll to the right/left, the next item will never in the center and I tried so many methods already still cannot solve the problem. Can anyone help me?
Below is the code.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
let cellCount = CGFloat(Config.banners.count)
if cellCount > 0 {
let flowLayout = collectionViewLayout as! UICollectionViewFlowLayout
let cellWidth = screenSize.width - flowLayout.minimumInteritemSpacing
flowLayout.itemSize = CGSize(width: cellWidth, height: 250)
flowLayout.minimumLineSpacing = -60
let totalCellWidth = cellWidth*cellCount
let totalSpacingWidth = 30 * (cellCount - 1)
let contentWidth = collectionView.frame.size.width
if (totalCellWidth < contentWidth) {
print("totalCellWidth < contentWidth")
let padding = (contentWidth - totalCellWidth) / 2.0
return UIEdgeInsets(top: 0, left: padding, bottom: 0, right: padding)
} else {
print("totalCellWidth > contentWidth")
return UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
}
}
return UIEdgeInsets.zero
}