added comments for SelfSizingCollectionView

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-05-26 16:35:02 -05:00
parent 814a048690
commit 8fd56aa99b

View File

@ -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