Last active
April 19, 2018 10:39
-
-
Save ramadana/286777e3767e757908abd21fdc80123c to your computer and use it in GitHub Desktop.
Snippet untuk create collection view cell bisa tertata pada screen.
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
... | |
// 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