Skip to content

Instantly share code, notes, and snippets.

@dejanskledar
Created May 10, 2018 07:23
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 dejanskledar/5a25b70a94f7ffda3558067f2ead5736 to your computer and use it in GitHub Desktop.
Save dejanskledar/5a25b70a94f7ffda3558067f2ead5736 to your computer and use it in GitHub Desktop.
This is a sample of a Centered CollectionView Flow Layout
class CollectionViewRow {
...
var rowWidth: CGFloat {
return attributes.reduce(0, { result, attribute -> CGFloat in
return result + attribute.frame.width
}) + CGFloat(attributes.count - 1) * spacing
}
func centerLayout(collectionViewWidth: CGFloat) {
let padding = (collectionViewWidth - rowWidth) / 2
var offset = padding
for attribute in attributes {
attribute.frame.origin.x = offset
offset += attribute.frame.width + spacing
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment