gpt4 book ai didi

ios - Collection View 更改间距

转载 作者:行者123 更新时间:2023-11-30 11:15:48 25 4
gpt4 key购买 nike

问题:如果流派或国家/地区( Collection View 单元格)的行数超过 1,则行的最后一个流派/国家/地区(单元格)会被剪切并导致空间增加。

ScreenShot

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
_ = cell.subviews.compactMap { $0.removeFromSuperview() }
cell.layer.borderColor = Colors.textPoster.color().cgColor
cell.layer.borderWidth = 0.5
cell.layer.cornerRadius = 3

let label = self.label()

if indexPath.section == 0 {
label.text = ganres[indexPath.row]
} else {
label.text = countries[indexPath.row]
}

label.sizeToFit()
cell.addSubview(label)
label.centerXAnchor.constraint(equalTo: cell.centerXAnchor).isActive = true
label.centerYAnchor.constraint(equalTo: cell.centerYAnchor).isActive = true
return cell
}

// MARK: UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 8
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 8
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: 0, height: 16)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let text = indexPath.section == 0 ? ganres[indexPath.row] : countries[indexPath.row]
let size = CGSize(width: text.width(withConstrainedHeight: .greatestFiniteMagnitude, font: labelFont) + space, height: text.height(withConstrainedWidth: .greatestFiniteMagnitude, font: labelFont) + space)

return size
}

最佳答案

我找到答案here 。这个FlowLayout使用minimumInteritemSpacing来进行左对齐。请务必自行设置。

class LeftAlignedCollectionViewFlowLayout: UICollectionViewFlowLayout {
var cache: [IndexPath : UICollectionViewLayoutAttributes] = [:]

override func prepare() {
cache = [:]
super.prepare()
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let originalAttributes = super.layoutAttributesForElements(in: rect) else {
return nil
}
var attributes = originalAttributes.map { $0.copy() as! UICollectionViewLayoutAttributes }
var attributesByGraphicalRow: [[UICollectionViewLayoutAttributes]] = []
var y: CGFloat = 0
var currentRow: [UICollectionViewLayoutAttributes] = []

func changeAttributesOfRow(_ row: [UICollectionViewLayoutAttributes]) {
var minX: CGFloat = 0

for rowAttribute in currentRow {
let oldFrame = rowAttribute.frame
let newOrigin = CGPoint(x: minX, y: oldFrame.origin.y)
let newFrame = CGRect(origin: newOrigin, size: oldFrame.size)
rowAttribute.frame = newFrame
minX += (newFrame.size.width + self.minimumInteritemSpacing)
}
}

for attribute in attributes {
if attribute.frame.origin.y > y {
// new row starts
changeAttributesOfRow(currentRow)
attributesByGraphicalRow.append(currentRow)
currentRow = []
y = attribute.frame.origin.y
}
currentRow.append(attribute)
}
if currentRow.count > 0 {
// last row isn't appended in for loop
changeAttributesOfRow(currentRow)
attributesByGraphicalRow.append(currentRow)
}

for attribute in attributes {
cache[attribute.indexPath] = attribute
}
return attributes
}

override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
if let attribute = cache[indexPath] {
return attribute
}
// now what??
return super.layoutAttributesForItem(at: indexPath)
}

}

关于ios - Collection View 更改间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51765760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com