UICollectionView 修改点击后颜色

一般UICollectionView点击后会显示默认的透明色,希望修改为特定的颜色,最终发现是设置背景色的view设置错了,应该是:[cell.contentView setBackgroundColor:[UIColor redColor]];
具体代码如下:

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
    //设置(Highlight)高亮下的颜色
    [cell.contentView setBackgroundColor:[UIColor redColor]];
}

- (void)collectionView:(UICollectionView *)collectionView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
    //设置(Nomal)正常状态下的颜色
    [cell.contentView setBackgroundColor:[UIColor blueColor]];
}

需要注意:是cell.contentView的背景颜色。

猜你喜欢

转载自blog.csdn.net/lllkey/article/details/82225803