UICollectionViewの内部にプログラムでUICollectionViewを作成していますが、入れ子になったUICollectionViewのセルの背景が黒色で、変更方法がわかりません。下に私のコードと、これまでに試したことの説明があります。セルの背後にあるUICollectionViewの背景が黒色です
アプリシミュレータ(APPの部分はアウトブロック):
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 .backgroundColor = UIColor.clear 3 backgroundView = nilの 2を設定します?私のネストされた細胞の背後にある背景はまだ黒で、私は理由を理解できません。
あなたは正しい方向に私を指摘してもらえますか?
ご協力いただきありがとうございます。