Skip to content

Instantly share code, notes, and snippets.

@ramadana
Last active April 19, 2018 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramadana/286777e3767e757908abd21fdc80123c to your computer and use it in GitHub Desktop.
Save ramadana/286777e3767e757908abd21fdc80123c to your computer and use it in GitHub Desktop.
Snippet untuk create collection view cell bisa tertata pada screen.
...
// Calculate cells spacing and size
var cellSize: CGFloat = 0
var isDecided = false
var cellSpacing:CGFloat = 1
let cellCounts:CGFloat = 3
while !isDecided {
let value = (UIScreen.main.bounds.width-cellSpacing*(cellCounts+1))/cellCounts
if value.truncatingRemainder(dividingBy: floor(value)) > 0 {
cellSpacing += 1
continue
}
cellSize = value
isDecided = true
}
// Setup cells spacing and insets
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: cellSpacing, left: cellSpacing, bottom: cellSpacing, right: cellSpacing)
layout.minimumInteritemSpacing = cellSpacing
layout.minimumLineSpacing = cellSpacing
collectionView.layout = layout
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment