Skip to content

Commit

Permalink
fix centering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lkzhao committed Jan 27, 2020
1 parent 7e30a31 commit 9a5dce4
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions CollectionKit2/Sources/CollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ open class CollectionView: UIScrollView {

public private(set) var lastLoadBounds: CGRect = .zero
public private(set) var contentOffsetChange: CGPoint = .zero

public var contentView: UIView? {
didSet {
oldValue?.removeFromSuperview()
if let contentView = contentView {
addSubview(contentView)
}
}

public var centerContentViewVertically = false
public var centerContentViewHorizontally = true
public var contentView: UIView? {
didSet {
oldValue?.removeFromSuperview()
if let contentView = contentView {
addSubview(contentView)
}
}
}

private var visibleIdentifiers: [String] = []

Expand All @@ -78,16 +80,21 @@ open class CollectionView: UIScrollView {

public func ensureZoomViewIsCentered() {
guard let contentView = contentView else { return }
let boundsSize = bounds.inset(by: adjustedContentInset)
let boundsSize: CGRect
if #available(iOS 11.0, *) {
boundsSize = bounds.inset(by: adjustedContentInset)
} else {
boundsSize = bounds.inset(by: contentInset)
}
var frameToCenter = contentView.frame

if frameToCenter.size.width < boundsSize.width {
if centerContentViewHorizontally, 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 {
if centerContentViewVertically, frameToCenter.size.height < boundsSize.height {
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) * 0.5
} else {
frameToCenter.origin.y = 0
Expand Down

0 comments on commit 9a5dce4

Please sign in to comment.