【发布时间】:2019-04-05 04:35:05
【问题描述】:
通过将集合视图布局的minimumLineSpacing 属性设置为负数,我已经让我的单元格重叠。但是,当我滚动并重新绘制单元格时,它们现在以相反的方向重叠。我在下面放了图片。
当集合视图滚动并重绘单元格时,如何保持单元格重叠,如第一张图片所示?
import UIKit
class PopularView: UIView {
let cellID = "cellID"
// MARK: - Views
let collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.minimumLineSpacing = -55 // -------Allows Overlap-----
layout.itemSize = CGSize(width: SCREEN_WIDTH, height: 185)
layout.minimumInteritemSpacing = 17
let view = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout)
view.backgroundColor = .white
return view
}()
// MARK: - Initializers
override init(frame: CGRect) {
super.init(frame: frame)
collectionView.dataSource = self
collectionView.register(PopularCell.self, forCellWithReuseIdentifier: cellID)
backgroundColor = .white
setupCollectionView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Setup
fileprivate func setupCollectionView() {
self.addSubview(collectionView)
collectionView.anchors(top: self.topAnchor, topPad: 0, bottom: self.bottomAnchor, bottomPad: 0, left: self.leftAnchor, leftPad: 0, right: self.rightAnchor, rightPad: 0, height: nil, width: nil)
collectionView.contentSize = CGSize(width: 700, height: 700)
}
}
extension PopularView: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 500
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! PopularCell
cell.background.backgroundColor = .random
return cell
}
}
【问题讨论】:
-
子类化
UICollectionViewLayout并为每个项目使用自定义布局属性。最小间距可能无济于事。 -
子类化
UICollectionViewLayout时我会编辑哪些特定属性? -
查看this链接,看看它是否满足您的要求。
-
绝对有帮助,但我仍然无法理解子类化布局属性的工作原理以及如何正确进行。
标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout