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

后端 未结 10 925
暗喜
暗喜 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:39

    This is a great tutorial for changing UICollectionView section color:

    Updated link, thanks to user @casamia

    http://www.ericjchapman.com/jekyll/update/ios/2014/11/10/ios-changing-section-background-color-in-UICollectionView.html

    Basically, we will have to subclass UICollectionViewLayoutAttributes, UICollectionReusableView and UICollectionViewLayout in order to create a instance UICollectionReusableView as section's background view.

    This is his result:

    Please follow the link for more details explanation.

    0 讨论(0)
  • 2020-12-13 18:43

    I have changed the background color of each section in a very simple manner in the following method: But I was not sure whether it is the right thing to do. But it did work.

    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
        FamilyCalendarCellItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"calendarItem" forIndexPath:indexPath];
        Event *event;
        _headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                       UICollectionElementKindSectionHeader withReuseIdentifier:@"EventHeader" forIndexPath:indexPath]; //headerView is declared as property of Collection Reusable View class
        if(indexPath.section==0) {
    
            cell.backgroundColor=[UIColor orangeColor];
            }
        else if(indexPath.section==1) {
    
            cell.backgroundColor=[UIColor yellowColor];
    
        }
    
        return cell;
    }
    
    0 讨论(0)
  • 2020-12-13 18:48

    In collection view every section can have a supplementary views, so put supplementary views for each section then set background color to supplementary views instead of section cells. I hope it will help.

    0 讨论(0)
  • 2020-12-13 18:48

    One of the classic approach is to create a Custom Supplementary Kind and provide your custom view in CollectionView Section background. It will give you the ability to customize section backgrounds. Refer to https://stackoverflow.com/a/63598373/14162081

    0 讨论(0)
  • 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
    
    }
    
    0 讨论(0)
  • 2020-12-13 18:50

    The new UICollectionViewCompositionalLayout introduced in iOS 13 have a property named decorationItems for adding decoration items conveniently, which you could use to add a background for the section.

    let section = NSCollectionLayoutSection(group: group)
    section.decorationItems = [
       NSCollectionLayoutDecorationItem.background(elementKind:"your identifier")
    ]
    
    0 讨论(0)
提交回复
热议问题