Giter Club home page Giter Club logo

scsectionbackground's Introduction

UICollectionView section background

This project shows how to apply colour to background sections (or cells) on a UICollectionView. The code is written in Swift, and inspired by Eric Chapman's article (Objective-C GitHub project). You can apply different colours according to the NSIndexPath of the cells. Here is an example of a UICollectionView 4 sections, 3 cells with green background on the even sections and 7 cells on a blue background on the odd ones.

Section Background in UICollectionView

How does it work?

The idea is to override UICollectionViewLayoutAttributes to add a color attribute. And then override UICollectionReusableView apply the color to the view background. Easy peasy :)

class SCSBCollectionViewLayoutAttributes : UICollectionViewLayoutAttributes { 
    var color: UIColor = UIColor.whiteColor()

    override func copyWithZone(zone: NSZone) -> AnyObject {
        let newAttributes: SCSBCollectionViewLayoutAttributes = super.copyWithZone(zone) as! SCSBCollectionViewLayoutAttributes
        newAttributes.color = self.color.copyWithZone(zone) as! UIColor
        return newAttributes
    }
}
class SCSBCollectionReusableView : UICollectionReusableView {
    override func applyLayoutAttributes(layoutAttributes: UICollectionViewLayoutAttributes) {       
        super.applyLayoutAttributes(layoutAttributes)
        let scLayoutAttributes = layoutAttributes as! SCSBCollectionViewLayoutAttributes self.backgroundColor =         scLayoutAttributes.color
    }
}

How to use it?

All this happens in the UICollectionViewFlowLayout used on your UIcollectionView. Here is an example of layoutAttributesForElementsInRect:

// Create decoration attributes
let decorationAttributes = SCSBCollectionViewLayoutAttributes(forDecorationViewOfKind: "sectionBackground", withIndexPath: attr.indexPath)
// Set the color(s)
if (attr.indexPath.section % 2 == 0) {
    decorationAttributes.color = UIColor.greenColor().colorWithAlphaComponent(0.5)
} else {
    decorationAttributes.color = UIColor.blueColor().colorWithAlphaComponent(0.5)
}

You will find all the details in the SectionBackgroundFlowLayout.swift file and if you want more details, please have a look at my the article I've written about it: How to create a Section Background in a UICollectionView in Swift.

Contact

You can ping me on Twitter @cath_schwz, open an issue on GitHub or leave a comment on my blog :)

scsectionbackground's People

Contributors

strawberrycode avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.