Skip to content

Instantly share code, notes, and snippets.

@daoseng33
Last active May 23, 2023 06:59
Show Gist options
  • Save daoseng33/2b99e6611a515ed20d1b0ab8b6c699f5 to your computer and use it in GitHub Desktop.
Save daoseng33/2b99e6611a515ed20d1b0ab8b6c699f5 to your computer and use it in GitHub Desktop.
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