如何解决如何根据swift中的文本紧密排列collectionview单元格
我给了 collectionview 约束
height = 50,leading = trailing = top = 10
我在单元格中的 containerview 中取了标签
单元容器视图约束
bottom = top = 5,horizontal center
容器视图的标签约束
leading = trailing = top = bottom = 5
didload 中的代码:
let serviceLayout = UICollectionViewFlowLayout()
serviceLayout.minimumInteritemSpacing = 0
serviceLayout.minimumLinespacing = 0
serviceLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
self.servicesCollectionView.collectionViewLayout = serviceLayout
我需要这样
我如何让 collectionview 单元彼此靠近,请帮忙
解决方法
在故事板中选择您的收藏视图
- 转到尺码检查员
- 如下设置滚动视图指示器左右插入
尝试使用这个自定义布局
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
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。