Ich erstelle eine UICollectionView innerhalb einer UICollectionView programmgesteuert, aber der Hintergrund hinter den Zellen meiner verschachtelten UICollectionView ist eine schwarze Farbe, und ich kann nicht herausfinden, wie man es ändert. Im Folgenden finden Sie meinen Code sowie eine Erläuterung dessen, was ich bisher versucht habe.UICollectionView Hintergrund hinter Zellen ist schwarz
App Simulator (Teile von APP blockiert):
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
Ich glaube, dass das Problem in meiner UICollectionView2Cell.swift-Datei auftritt. in den setupFrames() -Methode, habe ich versucht, die folgenden:
1) Einstellen des backgroundView = nil 2) backgroundView .backgroundColor = UIColor.clear 3) die Isopaque Eigenschaft auf "False"
jedoch? Der Hintergrund hinter meinen verschachtelten Zellen ist immer noch schwarz, und ich kann nicht herausfinden warum.
Könnt ihr mir bitte in die richtige Richtung zeigen?
Ihre Hilfe wird sehr geschätzt.