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

add sticky footer to UICollectionView in SWIFT

I have a UICollectionView with an x amount of rows that will increase if the user has more data to load and if they scroll down to load it.

Should I subclass UICollectionFlowLayout in order to make a custom footer?

:( 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)

    }
}

2 Answers

Depending on what you want to have in your "footer" you can just embed your table view in a tab Bar controller or a subclass of it.

tableView?

I'd like to add a enclosed navigation area where the user can swipe left or right within the sticky footer

Still looking into this :(