-4

如何将collectionview单元格背景的渐变颜色传递给自定义视图的其他视图控制器?

4

2 回答 2

0

您可以在您的 SecondViewController 中创建一个初始化程序,以便在呈现之前将您可能想要的颜色传递给那里。

init(color: UIColor) {
    super.init(nibName: nil, bundle: nil)
    self.color = color
}

然后,当您实例化您的 SecondViewController 并在其中使用颜色时。

于 2018-12-26T22:55:32.830 回答
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
    }

}
于 2018-12-26T12:08:52.757 回答