gpt4 book ai didi

swift - 单元格后面的 UICollectionView 背景为黑色

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

我正在以编程方式在 UICollectionView 内部创建 UICollectionView,但是嵌套 UICollectionView 单元格后面的背景是黑色,我不知道如何更改它。您将在下面找到我的代码,以及迄今为止我所尝试的内容的解释。

应用程序模拟器(应用程序的部分内容被屏蔽):

enter image description here

UICollectionView1.swift

private var collectionViewLayout: UICollectionViewFlowLayout? = nil
private var collectionView: UICollectionView? = nil

collectionViewLayout = UICollectionViewFlowLayout()
collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: 0, height: 0), collectionViewLayout: collectionViewLayout!)
collectionView?.backgroundColor = UIColor.white

collectionView?.register(UICollectionView1Cell.self, forCellWithReuseIdentifier: "cell")
collectionView?.dataSource = self
collectionView?.delegate = self

view.addSubview(collectionView!)

collectionView?.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
(collectionView?.topAnchor.constraint(equalTo: view.topAnchor, constant: 125))!,
(collectionView?.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -75))!,
(collectionView?.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0))!,
(collectionView?.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0))!
])

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! UICollectionViewCell
return cell
}

func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
let homeTestCollectionViewCell = cell as! HomeTestCollectionViewCell

switch(indexPath.row) {
case 0:
UICollectionView1Cell.cellLabel?.text = "Sponsored:"
UICollectionView1Cell.collectionViewIndex = indexPath.row
break
case 1:
UICollectionView1Cell.cellLabel?.text = "Trending Near You:"
UICollectionView1Cell.collectionViewIndex = indexPath.row
break
case 2:
UICollectionView1Cell.cellLabel?.text = "Hot:"
UICollectionView1Cell.collectionViewIndex = indexPath.row
break
default:
break
}
}

UICollectionView1Cell.swift

var cellLabel: UILabel? = nil
var subCollectionViewLayout: UICollectionViewFlowLayout? = nil
var subCollectionView: UICollectionView? = nil
var collectionViewIndex: Int?

cellLabel = UILabel()
cellLabel?.textColor = UIColor.red
addSubview(cellLabel!)
cellLabel?.translatesAutoresizingMaskIntoConstraints = false
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": cellLabel!]))

subCollectionViewLayout = UICollectionViewFlowLayout()
subCollectionView?.backgroundColor = UIColor.clear
subCollectionView = UICollectionView(frame: .zero, collectionViewLayout: subCollectionViewLayout!)
subCollectionView?.register(UICollectionView2Cell.self, forCellWithReuseIdentifier: "subCell")
subCollectionView?.dataSource = self
subCollectionView?.delegate = self
addSubview(subCollectionView!)
subCollectionView?.translatesAutoresizingMaskIntoConstraints = false
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[v0]-8-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": subCollectionView!]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0(20)]-12-[v1]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": cellLabel!, "v1": subCollectionView!]))

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let subCell = collectionView.dequeueReusableCell(withReuseIdentifier: "subCell", for: indexPath) as! UICollectionView2Cell
return subCell
}

UICollectionView2Cell.swift

var label: UILabel = UILabel()

override init(frame: CGRect) {
super.init(frame: frame)

setupFrames()
}

func setupFrames() {
backgroundColor = UIColor.white

label.textColor = UIColor.blue
addSubview(label)
label.translatesAutoresizingMaskIntoConstraints = false
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-2-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": label]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-2-[v0]-2-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": label]))
}

我相信这个问题发生在我的 UICollectionView2Cell.swift 文件中。在 setupFrames() 方法中,我尝试了以下操作:

1) 设置backgroundView = nil2)backgroundView?.backgroundColor = UIColor.clear3) 将 isOpaque 属性设置为“False”

但是,我的嵌套单元格后面的背景仍然是黑色,我不明白为什么。

你们能给我指出正确的方向吗?

非常感谢您的帮助。

最佳答案

我明白了。

在我的 UICollectionView1Cell.swift 文件中,我有以下代码:

subCollectionView?.backgroundColor = UIColor.clear
subCollectionView = UICollectionView(frame: .zero, collectionViewLayout: subCollectionViewLayout!)

我在 CollectionView 初始化之前设置了背景颜色。

关于swift - 单元格后面的 UICollectionView 背景为黑色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43954172/

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