单元格背后的UICollectionView背景为黑色

时间:2017-05-13 14:14:45

标签: swift

我正在以编程方式在UICollectionView内部创建一个UICollectionView,但是我嵌套的UICollectionView单元格背后的背景是黑色,我无法弄清楚如何更改它。下面你会找到我的代码,以及到目前为止我尝试过的解释。

App Simulator(应用程序部分被阻止):

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 = nil 2)backgroundView?.backgroundColor = UIColor.clear 3)将isOpaque属性设置为“False”

然而,我的嵌套单元格后面的背景仍然是黑色的,我无法找出原因。

请你指点我正确的方向吗?

非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我明白了。

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

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

我在collectionView甚至初始化之前设置了backgroundColor。