Created
May 10, 2018 07:23
-
-
Save dejanskledar/5a25b70a94f7ffda3558067f2ead5736 to your computer and use it in GitHub Desktop.
This is a sample of a Centered CollectionView Flow Layout
This file contains 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
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