问题
I have a UICollectionView
and only one section. Is there a way to set the background color of every row? (in my case it would be a gradient from black to white)
回答1:
CollectionViews don't really have a concept of a rows because it's designed to have a flexible layout.
Your best option is likely to have the background of the cells have your pattern and remove all the padding/margin between the cells.
This will be a problem if the last row isn't full, so if the number needs to be flexible you would have to add empty cells.
回答2:
You can set on color on cell using cell.backgroundColor = [UIColor magentaColor];
else for gradient you have to layer and set to cell like below code
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = cell.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[cell.layer insertSublayer:gradient atIndex:0];
来源:https://stackoverflow.com/questions/37351544/uicollectionview-how-to-set-background-color-of-a-row