Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Attempting to make a Sticky Footer for UICollectionView

What I am trying to achieve: A footer that sticks to the bottom of the screen at all times. This means no matter how much the user scrolls down the collection, they should still see the footer, kind of like a tab bar. Currently, the footer sticks, but is not visible unless you scroll all the way down.

My Current Code for my custom flow layout: :( Still not sure how to do this. My current code :

import UIKIt

class StickyFooter : UICollectionViewFlowLayout {


    var footerIsFound             : Bool = false
    var UICollectionAttributes    : [UICollectionViewLayoutAttributes]?


    override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
        return true
    }

 override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {


     UICollectionAttributes = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes]

    for  attributes in UICollectionAttributes! {

        if let type = attributes.representedElementKind {

            if type == UICollectionElementKindSectionFooter
         {
            footerIsFound = true
            updateFooter(attributes)
         }
        }
    }

    if (!self.footerIsFound) {

            let newItem = self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionFooter, atIndexPath : NSIndexPath(index: self.UICollectionAttributes!.count))

        }

            return UICollectionAttributes
        }


    override func layoutAttributesForSupplementaryViewOfKind(elementKind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {

        let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath : indexPath)

        attributes.size = CGSizeMake(self.collectionView!.bounds.size.width, 75);

        updateFooter(attributes)

        return attributes
    }

    func updateFooter(attributes : UICollectionViewLayoutAttributes){
        let currentBounds = self.collectionView?.bounds
        attributes.zIndex = 1024
        attributes.hidden = false

        let yOffset = currentBounds!.origin.y + currentBounds!.size.height - attributes.size.height/2.0

        attributes.center = CGPointMake(CGRectGetMidX(currentBounds!), yOffset)

    }
}

Mhm I guess I need to do more math

1 Answer

Nevermind, I figured it out. So if anyone is interested, I needed to set the footerIsFound to false every time the collection reloaded in order to force it readd the footer :)