Skip to content

Instantly share code, notes, and snippets.

@borisdipner
Created February 16, 2021 07:11
Show Gist options
  • Save borisdipner/beb9008583820461a4c43f7366a27363 to your computer and use it in GitHub Desktop.
Save borisdipner/beb9008583820461a4c43f7366a27363 to your computer and use it in GitHub Desktop.
class TagsLayout: UICollectionViewFlowLayout {
required override init() {super.init(); common()}
required init?(coder aDecoder: NSCoder) {super.init(coder: aDecoder); common()}
private func common() {
estimatedItemSize = UICollectionViewFlowLayout.automaticSize
minimumLineSpacing = 10
minimumInteritemSpacing = 10
}
override func layoutAttributesForElements(
in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let att = super.layoutAttributesForElements(in:rect) else {return []}
var x: CGFloat = sectionInset.left
var y: CGFloat = -1.0
for a in att {
if a.representedElementCategory != .cell { continue }
if a.frame.origin.y >= y { x = sectionInset.left }
a.frame.origin.x = x
x += a.frame.width + minimumInteritemSpacing
y = a.frame.maxY
}
return att
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment