diff --git a/Pod/Classes/Layouts/DisplaySwitchLayout.swift b/Pod/Classes/Layouts/DisplaySwitchLayout.swift index b287261..7275116 100644 --- a/Pod/Classes/Layouts/DisplaySwitchLayout.swift +++ b/Pod/Classes/Layouts/DisplaySwitchLayout.swift @@ -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 @@ -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") @@ -54,6 +60,7 @@ open class DisplaySwitchLayout: UICollectionViewLayout { override open func prepare() { super.prepare() + calculateMinGridColumns() baseLayoutAttributes = [DisplaySwitchLayoutAttributes]() // cells layout @@ -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