如何在UICollectionView上设置渐变固定背景?

nao*_*1ta 4 gradient objective-c ios uicollectionview

我使用以下代码.

CAGradientLayer* collectionRadient = [CAGradientLayer layer];
collectionRadient.bounds = self.collectionView.bounds;
collectionRadient.anchorPoint = CGPointZero;
collectionRadient.colors = [NSArray arrayWithObjects:(id)[startColor CGColor],(id)[endColor CGColor], nil];
[self.collectionView.layer insertSublayer:collectionRadient atIndex:0];
Run Code Online (Sandbox Code Playgroud)

但它在包含图像的细胞上绘制.所以没有显示细胞.

我想在Cells下绘制UICollectionView的渐变背景,并在滚动视图时修复它.请告诉我.

小智 12

试试这个......你必须指定一个视图来使用背景视图.

CAGradientLayer* collectionGradient = [CAGradientLayer layer];
collectionGradient.bounds = self.view.bounds;
collectionGradient.anchorPoint = CGPointZero;
collectionGradient.colors = [NSArray arrayWithObjects:(id)[[UIColor redColor] CGColor],(id)[[UIColor greenColor] CGColor], nil];
UIView *vv = [[UIView alloc] init];
vview.backgroundView = vv;
[vview.backgroundView.layer insertSublayer:collectionGradient atIndex:0];
Run Code Online (Sandbox Code Playgroud)