【发布时间】:2017-05-13 14:14:44
【问题描述】:
我正在以编程方式在 UICollectionView 内创建 UICollectionView,但是嵌套 UICollectionView 单元格后面的背景是黑色,我不知道如何更改它。您将在下面找到我的代码,以及迄今为止我尝试过的内容的说明。
App Simulator(部分应用被屏蔽):
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”
但是,我的嵌套单元格后面的背景仍然是黑色的,我不知道为什么。
请你们指点我正确的方向吗?
非常感谢您的帮助。
【问题讨论】:
标签: swift