Created
October 23, 2018 10:55
-
-
Save wanbok/da45365d509f053c78dea9b80ab3de2b to your computer and use it in GitHub Desktop.
UICollectionViewFlowLayout for something like a tag list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// LeftAlignedFlowLayout.swift | |
// | |
// Created by Wanbok Choi on 19/05/2017 | |
// | |
import UIKit | |
final class LeftAlignedFlowLayout: UICollectionViewFlowLayout { | |
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? { | |
let attributes = super.layoutAttributesForElements(in: rect) | |
var leftMargin = sectionInset.left | |
var maxY: CGFloat = .leastNormalMagnitude | |
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