如何将collectionview单元格背景的渐变颜色传递给自定义视图的其他viewcontroller?
答案 0 :(得分:0)
如果要更改背景渐变颜色的“其他” ViewController是容纳UICollectionView的颜色,请使用下面的代码...如果是其他ViewController,则需要获取该ViewController的实例你想改变...
class MyCollectionViewCell: UICollectionViewCell {
var delegate: MyCollectionViewCellDelegate?
func funcThatChangesColorInOtherViewColtroller() {
let firstGradientColor = UIColor.red
let secondGradientColor = UIColor.blue
delegate?.viewControllerShouldUpdateBackgroundToColors(firstColor: firstGradientColor, secondColor: secondGradientColor)
}
}
protocol MyCollectionViewCellDelegate {
func viewControllerShouldUpdateBackgroundToColors(firstColor: UIColor, secondColor: UIColor)
}
class OtherViewController: UICollectionViewDataSource, MyCollectionViewCellDelegate {
...
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ReuseIdentifier", for: indexPath) as! MyCollectionViewCell
cell.delegate = self
return cell
}
func viewControllerShouldUpdateBackgroundToColors(firstColor: UIColor, secondColor: UIColor) {
//Set your VCs Background Gradient to the Colors in
}
}
答案 1 :(得分:0)
您可以在SecondViewController中创建一个初始化程序,以便在显示该颜色之前将其传递给您所需的颜色。
init(color: UIColor) {
super.init(nibName: nil, bundle: nil)
self.color = color
}
然后,当您实例化SecondViewController并在其中使用颜色时。