Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
- override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
- guard let superArray = super.layoutAttributesForElements(in: rect),
- let attributes = NSArray(array: superArray, copyItems: true) as? [UICollectionViewLayoutAttributes] else { return nil }
- var leftMargin = sectionInset.left
- var maxY: CGFloat = -1.0
- attributes.forEach { layoutAttribute in
- guard layoutAttribute.representedElementCategory == .cell else {
- return
- }
- guard layoutAttribute.indexPath.section == 0 else {
- return
- }
- if layoutAttribute.frame.origin.y >= maxY {
- leftMargin = sectionInset.left
- }
- layoutAttribute.frame.origin.x = leftMargin
- leftMargin += layoutAttribute.frame.width + ((self.collectionView?.delegate as? UICollectionViewDelegateFlowLayout)?.collectionView?(self.collectionView!, layout: self, minimumInteritemSpacingForSectionAt: 0) ?? 0 )
- maxY = max(layoutAttribute.frame.maxY , maxY)
- }
- return attributes
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement