mvm_core_ui/MVMCoreUI/BaseClasses/UICollectionViewLeftAlignedLayout.swift
2020-06-08 15:20:37 +05:30

37 lines
1.3 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 newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
var leftMargin: CGFloat = 0.0;
let leftPadding: CGFloat = 0
let interItemSpacing = minimumInteritemSpacing
var maxY: CGFloat = -1.0
for attribute in attributes {
if attribute.frame.origin.y >= maxY {
leftMargin = leftPadding
} else {
var newLeftAlignedFrame = attribute.frame
newLeftAlignedFrame.origin.x = leftMargin
attribute.frame = newLeftAlignedFrame
}
attribute.frame.origin.x = leftMargin
leftMargin += attribute.frame.width + interItemSpacing
maxY = max(attribute.frame.maxY, maxY)
newAttributesForElementsInRect.append(attribute)
}
return newAttributesForElementsInRect
}
}