Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic grid columns #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions Pod/Classes/Layouts/DisplaySwitchLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ private let GridLayoutCountOfColumns = 3

open class DisplaySwitchLayout: UICollectionViewLayout {

fileprivate let numberOfColumns: Int
fileprivate var numberOfColumns: Int
fileprivate var minGridWidth: CGFloat?
fileprivate let cellPadding: CGFloat = 6.0
fileprivate let staticCellHeight: CGFloat
fileprivate let nextLayoutStaticCellHeight: CGFloat
Expand All @@ -36,14 +37,19 @@ open class DisplaySwitchLayout: UICollectionViewLayout {

// MARK: - Lifecycle

public init(staticCellHeight: CGFloat, nextLayoutStaticCellHeight: CGFloat, layoutState: LayoutState) {
public init(staticCellHeight: CGFloat, nextLayoutStaticCellHeight: CGFloat, layoutState: LayoutState, minGridWidth: CGFloat? = nil) {
self.staticCellHeight = staticCellHeight
self.numberOfColumns = layoutState == .list ? ListLayoutCountOfColumns : GridLayoutCountOfColumns
self.layoutState = layoutState
self.nextLayoutStaticCellHeight = nextLayoutStaticCellHeight

self.minGridWidth = minGridWidth
super.init()
}

func calculateMinGridColumns() {
guard let minGridWidth = minGridWidth else { return }
numberOfColumns = layoutState == .grid ? Int(contentWidth / minGridWidth) : ListLayoutCountOfColumns
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
Expand All @@ -54,6 +60,7 @@ open class DisplaySwitchLayout: UICollectionViewLayout {
override open func prepare() {
super.prepare()

calculateMinGridColumns()
baseLayoutAttributes = [DisplaySwitchLayoutAttributes]()

// cells layout
Expand Down Expand Up @@ -100,10 +107,9 @@ open class DisplaySwitchLayout: UICollectionViewLayout {
return DisplaySwitchLayoutAttributes.self
}

// Fix bug with content offset
override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
override open func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint, withScrollingVelocity velocity: CGPoint) -> CGPoint {
let previousContentOffsetPoint = previousContentOffset?.cgPointValue
let superContentOffset = super.targetContentOffset(forProposedContentOffset: proposedContentOffset)
let superContentOffset = super.targetContentOffset(forProposedContentOffset: proposedContentOffset, withScrollingVelocity: velocity)
if let previousContentOffsetPoint = previousContentOffsetPoint {
if previousContentOffsetPoint.y == 0 {
return previousContentOffsetPoint
Expand Down