UICollectionView - How to set background color of a row

删除回忆录丶 提交于 2021-02-08 07:32:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!