【发布时间】:2021-03-01 09:55:29
【问题描述】:
我正在使用UICollectionView 下面是我的代码,如何删除垂直间距?
我尝试设置UICollectionViewFlowLayout,但没有成功。
class ViewController: UIViewController {
@IBOutlet weak var cv: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
cv.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
let xx : CGFloat = (UIScreen.main.bounds.width / 8)
let yy : CGFloat = 6.0
layout.itemSize = CGSize(width: xx, height: yy)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 1
cv!.collectionViewLayout = layout
}
}
extension ViewController: UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 70
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
if (indexPath.row/2 == 0) {
cell.backgroundColor = UIColor.blue.withAlphaComponent(12)
}
else if (indexPath.row/4 == 0) {
cell.backgroundColor = UIColor.blue.withAlphaComponent(0.5)
}
else{
cell.backgroundColor = UIColor.blue.withAlphaComponent(0.1)
}
return cell
}
}
【问题讨论】:
标签: ios swift uicollectionview uicollectionviewcell