Last active
November 1, 2022 07:54
-
-
Save abhi21git/d0aadde3f14ecf464fdc6b0cf8849f98 to your computer and use it in GitHub Desktop.
Dynamic cell size based on screenWidth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { | |
let totalAvailableWidth = collectionView.bounds.width | |
let padding: CGFloat = 25 | |
let interSpace: CGFloat = 15 | |
let numOfCellInaRow: CGFloat = 3 | |
let cellWidth: CGFloat = (totalAvailableWidth - (padding * 2) - (interSpace * (numOfCellInaRow - 1))) / numOfCellInaRow | |
/// padding * 2 = leading + trailing space, (interSpace * (numOfCellInaRow - 1)) = totalInterspacing as 3 cells will have 2 interspace | |
let cellSize = CGSize(width: cellWidth, height: cellWidth) | |
return cellSize | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment