【发布时间】:2016-10-02 09:29:41
【问题描述】:
我一直在尝试创建一个在两行中水平显示标签的集合视图。用户可以水平滚动以查看更多标签。与 Bandcamp 应用程序中的过滤器完全一样 https://itunes.apple.com/us/app/bandcamp/id706408639?mt=8
我找到了一个很好的教程,通过自定义 UICollectionViewFlowLayout 来做类似的事情。 https://codentrick.com/create-a-tag-flow-layout-with-uicollectionview/
但是,本教程适用于垂直集合视图,根据需要创建行。我需要的是标签向右流动,并将布局限制为两行。
这是教程中 UICollectionViewFlowLayout 的 sn-p
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
}
}
谢谢!
【问题讨论】:
标签: ios swift uicollectionview horizontal-scrolling uicollectionviewlayout