From ba33824576704b02c34116b1d3b906732bfe7c22 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Tue, 13 Feb 2024 16:45:17 -0600 Subject: [PATCH 1/3] fixed bug for ButtonGroupPositionLayout - take the min between collectionViewWidth and button width Signed-off-by: Matt Bruce --- .../Buttons/ButtonGroup/ButtonGroupPositionLayout.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift index c1a315eb..040ac1b3 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift @@ -229,7 +229,7 @@ class ButtonGroupPositionLayout: UICollectionViewLayout { // create the custom layout attribute let attributes = ButtonLayoutAttributes(spacing: itemSpacing, button: itemButtonBase, forCellWith: indexPath) - attributes.frame = CGRect(x: 0, y: 0, width: itemSize.width, height: itemSize.height) + attributes.frame = CGRect(x: 0, y: 0, width: min(itemSize.width, collectionViewWidth), height: itemSize.height) // add it to the array rows.last?.add(attribute: attributes) From 0aea28663099d94f2cb1d0f6af7c1ae2751ff8e2 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 14 Feb 2024 09:05:41 -0600 Subject: [PATCH 2/3] pushing the heightConstraint into the selfsizingcv Signed-off-by: Matt Bruce --- VDS/Classes/SelfSizingCollectionView.swift | 8 ++++++++ VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift | 4 ---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/VDS/Classes/SelfSizingCollectionView.swift b/VDS/Classes/SelfSizingCollectionView.swift index 3a99404a..c50082c4 100644 --- a/VDS/Classes/SelfSizingCollectionView.swift +++ b/VDS/Classes/SelfSizingCollectionView.swift @@ -7,6 +7,7 @@ import Foundation import UIKit +import Combine /// UICollectionView subclassed to deal with Changing the size of itself based on its children and layout and changes of its contentSize. @objc(VDSSelfSizingCollectionView) @@ -34,10 +35,13 @@ public final class SelfSizingCollectionView: UICollectionView { // MARK: - Private Properties //-------------------------------------------------- private var contentSizeObservation: NSKeyValueObservation? + private var collectionViewHeight: NSLayoutConstraint? + private var anyCancellable: AnyCancellable? //-------------------------------------------------- // MARK: - Overrides //-------------------------------------------------- + /// The natural size for the receiving view, considering only properties of the view itself. public override var intrinsicContentSize: CGSize { let contentSize = self.contentSize @@ -63,12 +67,16 @@ public final class SelfSizingCollectionView: UICollectionView { //ensure autoLayout uses intrinsic height setContentHuggingPriority(.required, for: .vertical) setContentCompressionResistancePriority(.required, for: .vertical) + collectionViewHeight = heightAnchor.constraint(equalToConstant: 0).activate() // Observing the value of contentSize seems to be the only reliable way to get the contentSize after the collection view lays out its subviews. self.contentSizeObservation = self.observe(\.contentSize, options: [.old, .new]) { [weak self] _, change in // If we don't specify `options: [.old, .new]`, the change.oldValue and .newValue will always be `nil`. if change.newValue != change.oldValue { self?.invalidateIntrinsicContentSize() + if let height = change.newValue?.height { + self?.collectionViewHeight?.constant = height + } } } } diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift index 113d19f5..a80594fc 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroup.swift @@ -103,7 +103,6 @@ open class ButtonGroup: View { //-------------------------------------------------- // MARK: - Private Properties //-------------------------------------------------- - fileprivate var collectionViewHeight: NSLayoutConstraint? fileprivate lazy var positionLayout = ButtonGroupPositionLayout().with { $0.position = .center @@ -132,8 +131,6 @@ open class ButtonGroup: View { super.setup() addSubview(collectionView) collectionView.pinToSuperView() - collectionViewHeight = heightAnchor.constraint(equalToConstant: VDS.Button.Size.large.height) - collectionViewHeight?.activate() } //-------------------------------------------------- @@ -184,7 +181,6 @@ open class ButtonGroup: View { DispatchQueue.main.async { [weak self] in guard let self else { return } self.collectionView.collectionViewLayout.invalidateLayout() - self.collectionViewHeight?.constant = self.collectionView.intrinsicContentSize.height } } } From e9df7018363f38d57ce2f187b33a729c7282841a Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Wed, 14 Feb 2024 09:32:14 -0600 Subject: [PATCH 3/3] fixed other issue with button group layout Signed-off-by: Matt Bruce --- .../Buttons/ButtonGroup/ButtonGroupPositionLayout.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift index 040ac1b3..353bd4f5 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift @@ -193,6 +193,9 @@ class ButtonGroupPositionLayout: UICollectionViewLayout { // get the rect size of the button itemSize = delegate.collectionView(collectionView, sizeForItemAtIndexPath: indexPath) + // ensure the width is not greater than the collectionViewWidth + itemSize.width = min(itemSize.width, collectionViewWidth) + // determine if the current button will fit in the row let rowItemCount = rows.last?.attributes.count ?? 0 @@ -229,7 +232,7 @@ class ButtonGroupPositionLayout: UICollectionViewLayout { // create the custom layout attribute let attributes = ButtonLayoutAttributes(spacing: itemSpacing, button: itemButtonBase, forCellWith: indexPath) - attributes.frame = CGRect(x: 0, y: 0, width: min(itemSize.width, collectionViewWidth), height: itemSize.height) + attributes.frame = CGRect(x: 0, y: 0, width: itemSize.width, height: itemSize.height) // add it to the array rows.last?.add(attribute: attributes)