How to change background color of a whole section in UICollectionView?

后端 未结 10 933
暗喜
暗喜 2020-12-13 18:21

In UICollectionView, I want to give the whole section a uniform background color, instead of for a single cell or for the whole collection view.

I don\'t see any de

10条回答
  •  轮回少年
    2020-12-13 18:49

    Its very simple just use this default UICollectionViewDelegate's method, it will works

    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        print(indexPath.item)
    
        let evenSectionColor = UIColor.clear
        let oddSectionColor = UIColor.white
        cell.contentView.backgroundColor = (indexPath.item % 2 == 0) ? evenSectionColor : oddSectionColor
    
    }
    

提交回复
热议问题