From 8fd56aa99bc9895fb7c04fca519175b67ab78c85 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 26 May 2023 16:35:02 -0500 Subject: [PATCH] added comments for SelfSizingCollectionView Signed-off-by: Matt Bruce --- VDS/Classes/SelfSizingCollectionView.swift | 28 +++++++++++++++------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/VDS/Classes/SelfSizingCollectionView.swift b/VDS/Classes/SelfSizingCollectionView.swift index 55c3962b..dbedd80e 100644 --- a/VDS/Classes/SelfSizingCollectionView.swift +++ b/VDS/Classes/SelfSizingCollectionView.swift @@ -9,12 +9,19 @@ import Foundation import UIKit @objc(VDSSelfSizingCollectionView) +/// UICollectionView subclassed used to deal with Changing the size of itself based on its children and layout and changes of its contentSize public final class SelfSizingCollectionView: UICollectionView { private var contentSizeObservation: NSKeyValueObservation? + //-------------------------------------------------- // MARK: - Lifecycle - + //-------------------------------------------------- + + /// Initializer + /// - Parameters: + /// - frame: Frame needed + /// - layout: Layout used for this CollectionView public override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) { super.init(frame: frame, collectionViewLayout: layout) self.setupContentSizeObservation() @@ -25,14 +32,18 @@ public final class SelfSizingCollectionView: UICollectionView { self.setupContentSizeObservation() } + //-------------------------------------------------- // MARK: - UIView + //-------------------------------------------------- + public override var intrinsicContentSize: CGSize { let contentSize = self.contentSize //print(#function, contentSize) return CGSize(width: UIView.noIntrinsicMetric, height: contentSize.height) } - + + /// Overridden to deal with Appearance Changes public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) { //print(type(of: self), #function) super.traitCollectionDidChange(previousTraitCollection) @@ -43,13 +54,9 @@ public final class SelfSizingCollectionView: UICollectionView { } } - public override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize { - let size = super.systemLayoutSizeFitting(targetSize, withHorizontalFittingPriority: horizontalFittingPriority, verticalFittingPriority: verticalFittingPriority) - //print(type(of: self), #function, targetSize, "->", size) - return size - } - + //-------------------------------------------------- // MARK: - Private + //-------------------------------------------------- private func setupContentSizeObservation() { // Observing the value of contentSize seems to be the only reliable way to get the contentSize after the collection view lays out its subviews. @@ -63,7 +70,10 @@ public final class SelfSizingCollectionView: UICollectionView { } extension UITraitCollection { - + + /// Used within SelfSizingCollectionView to determine if there is an appearance change + /// - Parameter traitCollection: TraitCollection to compare + /// - Returns: True/False based on the trailCollection passed in public func hasDifferentTextAppearance(comparedTo traitCollection: UITraitCollection?) -> Bool { var result = self.preferredContentSizeCategory != traitCollection?.preferredContentSizeCategory result = result || self.legibilityWeight != traitCollection?.legibilityWeight