From 7e30a316ecf25a677ea941069917e66d52c38737 Mon Sep 17 00:00:00 2001 From: Luke Zhao Date: Sat, 28 Dec 2019 10:06:08 -0800 Subject: [PATCH] fix zoom --- CollectionKit2/Sources/CollectionView.swift | 22 +++++++++++++++++++ .../Sources/Layout/FlowLayout.swift | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CollectionKit2/Sources/CollectionView.swift b/CollectionKit2/Sources/CollectionView.swift index d9ceacb9..65801037 100644 --- a/CollectionKit2/Sources/CollectionView.swift +++ b/CollectionKit2/Sources/CollectionView.swift @@ -72,6 +72,28 @@ open class CollectionView: UIScrollView { _loadCells(reloading: false) } contentView?.frame = CGRect(origin: .zero, size: contentSize) + ensureZoomViewIsCentered() + } + + + public func ensureZoomViewIsCentered() { + guard let contentView = contentView else { return } + let boundsSize = bounds.inset(by: adjustedContentInset) + var frameToCenter = contentView.frame + + if frameToCenter.size.width < boundsSize.width { + frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) * 0.5 + } else { + frameToCenter.origin.x = 0 + } + + if frameToCenter.size.height < boundsSize.height { + frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) * 0.5 + } else { + frameToCenter.origin.y = 0 + } + + contentView.frame = frameToCenter } public func setNeedsReload() { diff --git a/CollectionKit2/Sources/Layout/FlowLayout.swift b/CollectionKit2/Sources/Layout/FlowLayout.swift index fd5cf278..79e4e2a1 100644 --- a/CollectionKit2/Sources/Layout/FlowLayout.swift +++ b/CollectionKit2/Sources/Layout/FlowLayout.swift @@ -8,7 +8,7 @@ import UIKit -public class FlowLayout: SortedLayoutProvider { +open class FlowLayout: SortedLayoutProvider { public var lineSpacing: CGFloat public var interitemSpacing: CGFloat