Skip to content

Commit

Permalink
- tested with headers/footers
Browse files Browse the repository at this point in the history
  • Loading branch information
romansorochak committed Jul 4, 2017
1 parent 32152bd commit 115a990
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 22 deletions.
4 changes: 4 additions & 0 deletions PinterestLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
640095F71F0BC37F006AE391 /* CollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 640095F61F0BC37F006AE391 /* CollectionViewCell.swift */; };
644F27AA1F0BF039004AD5AD /* RoundedCornersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */; };
B572E5EF1F0C20D100540F7C /* PinterestHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = B572E5EE1F0C20D100540F7C /* PinterestHeader.swift */; };
B5AFCBAC1F0BB16F00CB71B5 /* PinterestLayout.h in Headers */ = {isa = PBXBuildFile; fileRef = B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */; settings = {ATTRIBUTES = (Public, ); }; };
B5CF9EDE1F0BB19500DAA3B7 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CF9EDD1F0BB19500DAA3B7 /* AppDelegate.swift */; };
B5CF9EE01F0BB19500DAA3B7 /* PinterestVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5CF9EDF1F0BB19500DAA3B7 /* PinterestVC.swift */; };
Expand Down Expand Up @@ -47,6 +48,7 @@
/* Begin PBXFileReference section */
640095F61F0BC37F006AE391 /* CollectionViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CollectionViewCell.swift; sourceTree = "<group>"; };
644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RoundedCornersView.swift; sourceTree = "<group>"; };
B572E5EE1F0C20D100540F7C /* PinterestHeader.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinterestHeader.swift; sourceTree = "<group>"; };
B5AFCBA71F0BB16F00CB71B5 /* PinterestLayout.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PinterestLayout.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B5AFCBAA1F0BB16F00CB71B5 /* PinterestLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PinterestLayout.h; sourceTree = "<group>"; };
B5AFCBAB1F0BB16F00CB71B5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -94,6 +96,7 @@
children = (
640095F61F0BC37F006AE391 /* CollectionViewCell.swift */,
644F27A91F0BF039004AD5AD /* RoundedCornersView.swift */,
B572E5EE1F0C20D100540F7C /* PinterestHeader.swift */,
);
name = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -285,6 +288,7 @@
B5CF9EDE1F0BB19500DAA3B7 /* AppDelegate.swift in Sources */,
640095F71F0BC37F006AE391 /* CollectionViewCell.swift in Sources */,
644F27AA1F0BF039004AD5AD /* RoundedCornersView.swift in Sources */,
B572E5EF1F0C20D100540F7C /* PinterestHeader.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
71 changes: 53 additions & 18 deletions PinterestLayout/PinterestLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import UIKit

//MARK: CollectionViewLayoutDelegate

public protocol CollectionViewLayoutDelegate {
@objc public protocol CollectionViewLayoutDelegate {

func collectionView(collectionView: UICollectionView,
sizeForSectionHeaderViewForSection section: Int) -> CGSize
@objc optional func collectionView(collectionView: UICollectionView,
sizeForSectionHeaderViewForSection section: Int) -> CGSize

@objc optional func collectionView(collectionView: UICollectionView,
sizeForSectionFooterViewForSection section: Int) -> CGSize

func collectionView(collectionView: UICollectionView,
heightForImageAtIndexPath indexPath: IndexPath,
Expand Down Expand Up @@ -94,6 +97,12 @@ public class PinterestLayout: UICollectionViewLayout {
}


public override func invalidateLayout() {
super.invalidateLayout()

cache.removeAll()
}

override public func prepare() {
if cache.isEmpty {
let collumnWidth = contentWidth / CGFloat(numberOfColumns)
Expand All @@ -108,24 +117,28 @@ public class PinterestLayout: UICollectionViewLayout {
for section in 0..<numberOfSections {
let numberOfItems = self.numberOfItems(inSection: section)

let headerY = contentHeight
let headerSize = delegate.collectionView(
if let headerSize = delegate.collectionView?(
collectionView: collectionView,
sizeForSectionHeaderViewForSection: section
)
let headerX = (contentWidth - headerSize.width) / 2
let headerFrame = CGRect(
origin: CGPoint(x: headerX, y: headerY),
size: headerSize
)
let attributes = PinterestLayoutAttributes(
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
with: IndexPath(item: 0, section: section)
)
attributes.frame = headerFrame
cache.append(attributes)
) {
let headerX = (contentWidth - headerSize.width) / 2
let headerFrame = CGRect(
origin: CGPoint(
x: headerX,
y: contentHeight
),
size: headerSize
)
let headerAttributes = PinterestLayoutAttributes(
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
with: IndexPath(item: 0, section: section)
)
headerAttributes.frame = headerFrame
cache.append(headerAttributes)

contentHeight = headerFrame.maxY
}

contentHeight = headerFrame.maxY
var yOffsets = [CGFloat](
repeating: contentHeight,
count: numberOfColumns
Expand Down Expand Up @@ -166,6 +179,28 @@ public class PinterestLayout: UICollectionViewLayout {
contentHeight = max(contentHeight, frame.maxY)
yOffsets[column] = yOffsets[column] + cellHeight
}

if let footerSize = delegate.collectionView?(
collectionView: collectionView,
sizeForSectionFooterViewForSection: section
) {
let footerX = (contentWidth - footerSize.width) / 2
let footerFrame = CGRect(
origin: CGPoint(
x: footerX,
y: contentHeight
),
size: footerSize
)
let footerAttributes = PinterestLayoutAttributes(
forSupplementaryViewOfKind: UICollectionElementKindSectionFooter,
with: IndexPath(item: 0, section: section)
)
footerAttributes.frame = footerFrame
cache.append(footerAttributes)

contentHeight = footerFrame.maxY
}
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions PinterestLayoutExample/CollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var imageViewHeightLayoutConstraint: NSLayoutConstraint!
@IBOutlet weak var descriptionLabel: UILabel!


var image: UIImage? {
didSet {
if let image = image {
Expand All @@ -28,7 +29,8 @@ class CollectionViewCell: UICollectionViewCell {

override func apply(_ layoutAttributes: UICollectionViewLayoutAttributes) {
super.apply(layoutAttributes)
let attributes = layoutAttributes as! PinterestLayoutAttributes
imageViewHeightLayoutConstraint.constant = attributes.imageHeight
if let attributes = layoutAttributes as? PinterestLayoutAttributes {
imageViewHeightLayoutConstraint.constant = attributes.imageHeight
}
}
}
15 changes: 15 additions & 0 deletions PinterestLayoutExample/PinterestHeader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// PinterestHeader.swift
// PinterestLayout
//
// Created by Roman Sorochak on 7/4/17.
// Copyright © 2017 MagicLab. All rights reserved.
//

import UIKit


class PinterestHeader: UICollectionReusableView {


}
40 changes: 38 additions & 2 deletions PinterestLayoutExample/PinterestVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ class PinterestVC: UICollectionViewController {

setupCollectionViewInsets()
setupLayout()

collectionView?.register(
PinterestHeader.self,
forSupplementaryViewOfKind: UICollectionElementKindSectionHeader,
withReuseIdentifier: "PinterestHeader"
)
collectionView?.register(
PinterestHeader.self,
forSupplementaryViewOfKind: UICollectionElementKindSectionFooter,
withReuseIdentifier: "PinterestHeader"
)
}


Expand All @@ -50,12 +61,28 @@ class PinterestVC: UICollectionViewController {

//MARK: UICollectionViewDataSource

extension PinterestVC: UICollectionViewDelegateFlowLayout {
extension PinterestVC {

override func numberOfSections(in collectionView: UICollectionView) -> Int {
return images.count
}

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(
ofKind: kind,
withReuseIdentifier: "PinterestHeader",
for: indexPath
)

if kind == UICollectionElementKindSectionHeader {
header.backgroundColor = UIColor.red
} else {
header.backgroundColor = UIColor.green
}

return header
}

override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images[section].count
}
Expand Down Expand Up @@ -84,9 +111,18 @@ extension PinterestVC: CollectionViewLayoutDelegate {

func collectionView(collectionView: UICollectionView,
sizeForSectionHeaderViewForSection section: Int) -> CGSize {
let width = collectionView.frame.width - collectionView.contentInset.left - collectionView.contentInset.right
return CGSize(
width: width,
height: 20
)
}

func collectionView(collectionView: UICollectionView,
sizeForSectionFooterViewForSection section: Int) -> CGSize {
return CGSize(
width: collectionView.frame.width,
height: 0
height: 20
)
}

Expand Down

0 comments on commit 115a990

Please sign in to comment.