Advertisement
Guest User

Untitled

a guest
Mar 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. import UIKit
  2.  
  3. class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
  4.  
  5. override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
  6. guard let superArray = super.layoutAttributesForElements(in: rect),
  7. let attributes = NSArray(array: superArray, copyItems: true) as? [UICollectionViewLayoutAttributes] else { return nil }
  8.  
  9. var leftMargin = sectionInset.left
  10. var maxY: CGFloat = -1.0
  11. attributes.forEach { layoutAttribute in
  12. guard layoutAttribute.representedElementCategory == .cell else {
  13. return
  14. }
  15. guard layoutAttribute.indexPath.section == 0 else {
  16. return
  17. }
  18. if layoutAttribute.frame.origin.y >= maxY {
  19. leftMargin = sectionInset.left
  20. }
  21.  
  22. layoutAttribute.frame.origin.x = leftMargin
  23.  
  24. leftMargin += layoutAttribute.frame.width + ((self.collectionView?.delegate as? UICollectionViewDelegateFlowLayout)?.collectionView?(self.collectionView!, layout: self, minimumInteritemSpacingForSectionAt: 0) ?? 0 )
  25. maxY = max(layoutAttribute.frame.maxY , maxY)
  26. }
  27.  
  28. return attributes
  29. }
  30.  
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement