Skip to content

Instantly share code, notes, and snippets.

@westerlund
Last active February 3, 2020 11:04
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westerlund/8eeee3c1cc88348becf8bf0146bafbce to your computer and use it in GitHub Desktop.
Save westerlund/8eeee3c1cc88348becf8bf0146bafbce to your computer and use it in GitHub Desktop.
// based on this answer 'http://stackoverflow.com/a/36016798'
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributes = super.layoutAttributesForElements(in: rect)
var leftMargin = sectionInset.left
var maxY: CGFloat = -1.0
attributes?.forEach { layoutAttribute in
if layoutAttribute.frame.origin.y >= maxY {
leftMargin = sectionInset.left
}
layoutAttribute.frame.origin.x = leftMargin
leftMargin += layoutAttribute.frame.width + minimumInteritemSpacing
maxY = max(layoutAttribute.frame.maxY , maxY)
}
return attributes
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment