如何根据 swift 中的文本紧密排列 collectionview 单元格
How to arrange collectionview cells closely according to text in swift
我已经给了collectionview约束
height = 50, leading = trailing = top = 10
我在单元格的 containerview 中取了标签
单元格容器视图约束
bottom = top = 5, horizontal center
使用 containerview 的标签约束
leading = trailing = top = bottom = 5
didload 中的代码:
let serviceLayout = UICollectionViewFlowLayout()
serviceLayout.minimumInteritemSpacing = 0
serviceLayout.minimumLineSpacing = 0
serviceLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
self.servicesCollectionView.collectionViewLayout = serviceLayout
我越来越这样我需要一个靠近另一个
我需要这样
如何让 collectionview 单元格彼此靠近,请帮忙
Select 你在故事板中的 collection 视图
- 转到尺寸检查器
- 设置scrollview指示器左右插入如下
尝试使用这个自定义布局
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let oldAttributes = super.layoutAttributesForElements(in: rect) else {
return super.layoutAttributesForElements(in: rect)
}
let spacing = CGFloat(6)
var newAttributes = [UICollectionViewLayoutAttributes]()
var leftMargin = self.sectionInset.left
for attributes in oldAttributes {
if attributes.representedElementCategory == .supplementaryView {
newAttributes.append(attributes)
} else {
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.width + spacing
newAttributes.append(attributes)
}
}
return newAttributes
}
}
我已经给了collectionview约束
height = 50, leading = trailing = top = 10
我在单元格的 containerview 中取了标签
单元格容器视图约束
bottom = top = 5, horizontal center
使用 containerview 的标签约束
leading = trailing = top = bottom = 5
didload 中的代码:
let serviceLayout = UICollectionViewFlowLayout()
serviceLayout.minimumInteritemSpacing = 0
serviceLayout.minimumLineSpacing = 0
serviceLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
self.servicesCollectionView.collectionViewLayout = serviceLayout
我越来越这样我需要一个靠近另一个
我需要这样
如何让 collectionview 单元格彼此靠近,请帮忙
Select 你在故事板中的 collection 视图
- 转到尺寸检查器
- 设置scrollview指示器左右插入如下
尝试使用这个自定义布局
class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let oldAttributes = super.layoutAttributesForElements(in: rect) else {
return super.layoutAttributesForElements(in: rect)
}
let spacing = CGFloat(6)
var newAttributes = [UICollectionViewLayoutAttributes]()
var leftMargin = self.sectionInset.left
for attributes in oldAttributes {
if attributes.representedElementCategory == .supplementaryView {
newAttributes.append(attributes)
} else {
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.width + spacing
newAttributes.append(attributes)
}
}
return newAttributes
}
}