I've been trying to create a collection view that presents tags horizontally, in two row. The user can than scroll horizontally to view more tags. Exactly like the filters at the Bandcamp app https://itunes.apple.com/us/app/bandcamp/id706408639?mt=8
I found a very good tutorial on how to do something similar, by customizing UICollectionViewFlowLayout. https://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/
However, this tutorial is meant for a vertical collection view, creating rows as needed. What I need is for the tags to flow right, and constrain the layout to two rows.
This is the snippet of the UICollectionViewFlowLayout from the tutorial
class FlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributesForElementsInRect = super.layoutAttributesForElementsInRect(rect)
var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
var leftMargin: CGFloat = 0.0;
for attributes in attributesForElementsInRect! {
if (attributes.frame.origin.x == self.sectionInset.left) {
leftMargin = self.sectionInset.left
} else {
var newLeftAlignedFrame = attributes.frame
newLeftAlignedFrame.origin.x = leftMargin
attributes.frame = newLeftAlignedFrame
}
leftMargin += attributes.frame.size.width + 8
newAttributesForElementsInRect.append(attributes)
}
return newAttributesForElementsInRect
}
}
Thx!
