52 lines
1.5 KiB
Swift
52 lines
1.5 KiB
Swift
//
|
|
// UICollectionViewLeftAlignedLayout.swift
|
|
// MVMCoreUI
|
|
//
|
|
// Created by Dhamodaram Nandi on 05/06/20.
|
|
// Copyright © 2020 Verizon Wireless. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
class UICollectionViewLeftAlignedLayout: UICollectionViewFlowLayout {
|
|
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
|
|
guard let attributes = super.layoutAttributesForElements(in: rect) else {
|
|
return nil
|
|
}
|
|
var rows = [Row]()
|
|
var currentRowY: CGFloat = -1
|
|
for attribute in attributes {
|
|
if currentRowY != attribute.frame.origin.y {
|
|
currentRowY = attribute.frame.origin.y
|
|
rows.append(Row(spacing: 10))
|
|
}
|
|
rows.last?.add(attribute: attribute)
|
|
}
|
|
rows.forEach { $0.tagLayout(collectionViewWidth: collectionView?.frame.width ?? 0) }
|
|
return rows.flatMap { $0.attributes }
|
|
}
|
|
}
|
|
|
|
class Row {
|
|
|
|
var attributes = [UICollectionViewLayoutAttributes]()
|
|
var spacing: CGFloat = 0
|
|
|
|
init(spacing: CGFloat) {
|
|
self.spacing = spacing
|
|
}
|
|
|
|
func add(attribute: UICollectionViewLayoutAttributes) {
|
|
attributes.append(attribute)
|
|
}
|
|
|
|
func tagLayout(collectionViewWidth: CGFloat) {
|
|
let padding = 8
|
|
var offset = padding
|
|
for attribute in attributes {
|
|
attribute.frame.origin.x = CGFloat(offset)
|
|
offset += Int(attribute.frame.width + spacing)
|
|
}
|
|
}
|
|
}
|