Merge branch 'test/cv' into mbruce/bugfixes

This commit is contained in:
Matt Bruce 2024-02-14 09:33:08 -06:00
commit 5f0ee106e0
2 changed files with 8 additions and 4 deletions

View File

@ -7,6 +7,7 @@
import Foundation import Foundation
import UIKit 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. /// UICollectionView subclassed to deal with Changing the size of itself based on its children and layout and changes of its contentSize.
@objc(VDSSelfSizingCollectionView) @objc(VDSSelfSizingCollectionView)
@ -34,10 +35,13 @@ public final class SelfSizingCollectionView: UICollectionView {
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
private var contentSizeObservation: NSKeyValueObservation? private var contentSizeObservation: NSKeyValueObservation?
private var collectionViewHeight: NSLayoutConstraint?
private var anyCancellable: AnyCancellable?
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Overrides // MARK: - Overrides
//-------------------------------------------------- //--------------------------------------------------
/// The natural size for the receiving view, considering only properties of the view itself. /// The natural size for the receiving view, considering only properties of the view itself.
public override var intrinsicContentSize: CGSize { public override var intrinsicContentSize: CGSize {
let contentSize = self.contentSize let contentSize = self.contentSize
@ -63,12 +67,16 @@ public final class SelfSizingCollectionView: UICollectionView {
//ensure autoLayout uses intrinsic height //ensure autoLayout uses intrinsic height
setContentHuggingPriority(.required, for: .vertical) setContentHuggingPriority(.required, for: .vertical)
setContentCompressionResistancePriority(.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. // 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 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 we don't specify `options: [.old, .new]`, the change.oldValue and .newValue will always be `nil`.
if change.newValue != change.oldValue { if change.newValue != change.oldValue {
self?.invalidateIntrinsicContentSize() self?.invalidateIntrinsicContentSize()
if let height = change.newValue?.height {
self?.collectionViewHeight?.constant = height
}
} }
} }
} }

View File

@ -103,7 +103,6 @@ open class ButtonGroup: View {
//-------------------------------------------------- //--------------------------------------------------
// MARK: - Private Properties // MARK: - Private Properties
//-------------------------------------------------- //--------------------------------------------------
fileprivate var collectionViewHeight: NSLayoutConstraint?
fileprivate lazy var positionLayout = ButtonGroupPositionLayout().with { fileprivate lazy var positionLayout = ButtonGroupPositionLayout().with {
$0.position = .center $0.position = .center
@ -132,8 +131,6 @@ open class ButtonGroup: View {
super.setup() super.setup()
addSubview(collectionView) addSubview(collectionView)
collectionView.pinToSuperView() 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 DispatchQueue.main.async { [weak self] in
guard let self else { return } guard let self else { return }
self.collectionView.collectionViewLayout.invalidateLayout() self.collectionView.collectionViewLayout.invalidateLayout()
self.collectionViewHeight?.constant = self.collectionView.intrinsicContentSize.height
} }
} }
} }