layout changes updated
This commit is contained in:
parent
2ec4881be6
commit
f4fa6d8291
@ -61,6 +61,7 @@ open class Tags: View, MFButtonProtocol {
|
||||
let layout = UICollectionViewLeftAlignedLayout()
|
||||
layout.estimatedItemSize = CGSize(width: 140, height: 40)
|
||||
layout.minimumLineSpacing = 24.0
|
||||
layout.minimumInteritemSpacing = 12.0
|
||||
return layout
|
||||
}
|
||||
|
||||
|
||||
@ -13,39 +13,24 @@ class UICollectionViewLeftAlignedLayout: UICollectionViewFlowLayout {
|
||||
guard let attributes = super.layoutAttributesForElements(in: rect) else {
|
||||
return nil
|
||||
}
|
||||
var rows = [Row]()
|
||||
var currentRowY: CGFloat = -1
|
||||
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 currentRowY != attribute.frame.origin.y {
|
||||
currentRowY = attribute.frame.origin.y
|
||||
rows.append(Row(spacing: 10))
|
||||
if attribute.frame.origin.y >= maxY {
|
||||
leftMargin = leftPadding
|
||||
} else {
|
||||
var newLeftAlignedFrame = attribute.frame
|
||||
newLeftAlignedFrame.origin.x = leftMargin
|
||||
attribute.frame = newLeftAlignedFrame
|
||||
}
|
||||
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)
|
||||
attribute.frame.origin.x = leftMargin
|
||||
leftMargin += attribute.frame.width + interItemSpacing
|
||||
maxY = max(attribute.frame.maxY, maxY)
|
||||
newAttributesForElementsInRect.append(attribute)
|
||||
}
|
||||
return newAttributesForElementsInRect
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user