Skip to content

Swift version of layout class #4

@ghost

Description

Following is the Swift version if you want to put it in your project:

import UIKit

class MainLayout: UICollectionViewFlowLayout {

/* in order to make the header and footer float (or stick to the
top and bottom of the screen), we need to invalidate the layout
on bounds change (when scrolling).
*/
override func shouldInvalidateLayoutForBoundsChange(newBounds: CGRect) -> Bool {
    return true
}

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    var allItems = super.layoutAttributesForElementsInRect(rect)

    var headerFound = false
    var footerFound = false
    for item in allItems! {
        if(item.representedElementKind == UICollectionElementKindSectionHeader){
            headerFound = true
            updateHeaderAttributes(item)
        }
        if(item.representedElementKind == UICollectionElementKindSectionFooter){
            footerFound = true
            updateFooterAttributes(item)
        }
    }

    // Flow layout will remove items from the list if they are supposed to be off screen, so we add them
    // back in in those cases.
    if(!headerFound){
        allItems!.append(self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionHeader, atIndexPath: NSIndexPath(index: 0))!)
    }
    if(!footerFound){
        allItems!.append(self.layoutAttributesForSupplementaryViewOfKind(UICollectionElementKindSectionFooter, atIndexPath: NSIndexPath(index: 0))!)
    }

    return allItems
}

/* now provide the layout attributes for your floating header/footer. */
override func layoutAttributesForSupplementaryViewOfKind(elementKind:String, atIndexPath indexPath:NSIndexPath) -> UICollectionViewLayoutAttributes? {

    let attributes = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: elementKind, withIndexPath: indexPath)
    attributes.size = CGSizeMake(self.collectionView!.bounds.size.width, 44)
    if (elementKind == UICollectionElementKindSectionHeader) {
        updateHeaderAttributes(attributes)
    } else {
        updateFooterAttributes(attributes)
    }
    return attributes
}

//pragma mark - make things stick

func updateHeaderAttributes(attributes:UICollectionViewLayoutAttributes) {
    let currentBounds = self.collectionView!.bounds
    attributes.zIndex = 1
    attributes.hidden = false
    let yCenterOffset = currentBounds.origin.y + CGFloat(attributes.size.height/2)
    attributes.center = CGPointMake(CGRectGetMidX(currentBounds), yCenterOffset)
}

func updateFooterAttributes(attributes:UICollectionViewLayoutAttributes) {
    let currentBounds = self.collectionView!.bounds
    attributes.zIndex = 1
    attributes.hidden = false
    let yCenterOffset = currentBounds.origin.y + currentBounds.size.height - CGFloat(attributes.size.height/2)
    attributes.center = CGPointMake(CGRectGetMidX(currentBounds), yCenterOffset)
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions