gpt4 book ai didi

ios - UICollectionViewBackgroundView问题

转载 作者:行者123 更新时间:2023-11-29 05:46:31 27 4
gpt4 key购买 nike

我正在尝试在 UICollectionView 后面添加自定义的交互式 View (由图片中的黄色背景表示)。我发现执行此操作的最简单方法是使用 UICollectionView 上的 backgroundView 属性。唯一的问题是,如果我在单元格之间添加间距,则背景 View 在间距之间可见。为了解决这个问题,我将单元格间距设为 0,并通过在单元格中的 UIImageView 周围添加边距(由紫色方 block 表示)来模仿黑色网格线,然后将背景设为细胞变黑。这对于单元格上方和下方的水平间距非常有效,但由于某种原因,通过它们之间的狭窄垂直线仍然可以看到背景 View 。我已确保单元格宽度恰好是 UICollectionView 的一半,并且 minimumLineSpacingminimumInteritemSpacing 均为 0.0。

enter image description here

最佳答案

我创建了与您相同的场景。对于我来说,使用下面的代码效果很好。

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
super.viewDidLoad()
let backView = UIView(frame: self.collectionView.bounds)
backView.backgroundColor = UIColor(red: 0.9372549057, green: 0.3490196168, blue: 0.1921568662, alpha: 1)
self.collectionView.backgroundView = backView
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 30
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
cell.backgroundColor = UIColor(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
cell.layer.borderWidth = 5.0
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.bounds.width / 2, height: collectionView.bounds.width / 2)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 100, left: 0, bottom: 100, right: 0)
}
}

唯一的是,我使用单元格的 borderWidth 进行说明。您可以根据您的要求进行更改。

enter image description here

关于ios - UICollectionViewBackgroundView问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56100517/

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