updated width/height for collectionview

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2022-08-31 16:12:19 -05:00
parent 61c5d5adf8
commit c05f35de03

View File

@ -62,8 +62,12 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
//--------------------------------------------------
public var label = Label()
private let cellSize: CGFloat = 48.0
private let labelSpacing: CGFloat = 24.0
private let labelHeight: CGFloat = 16.0
private let lineSpacing: CGFloat = 12.0
private let itemSpacing: CGFloat = 16.0
private var collectionViewHeight: NSLayoutConstraint?
private var collectionViewWidth: NSLayoutConstraint?
private lazy var collectionView: UICollectionView = {
let layout = UICollectionViewFlowLayout().with {
@ -74,6 +78,7 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
$0.backgroundColor = .clear
$0.showsHorizontalScrollIndicator = false
$0.showsVerticalScrollIndicator = false
$0.isScrollEnabled = false
$0.translatesAutoresizingMaskIntoConstraints = false
$0.register(CollectionViewCell<ModelHandlerType>.self, forCellWithReuseIdentifier: "collectionViewCell")
}
@ -105,15 +110,45 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
label.topAnchor.constraint(equalTo: topAnchor),
label.leadingAnchor.constraint(equalTo: leadingAnchor),
label.trailingAnchor.constraint(equalTo: trailingAnchor),
collectionView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: 24),
label.heightAnchor.constraint(equalToConstant: labelHeight),
collectionView.topAnchor.constraint(equalTo: label.bottomAnchor, constant: labelSpacing),
collectionView.leadingAnchor.constraint(equalTo: leadingAnchor),
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor),
collectionView.heightAnchor.constraint(greaterThanOrEqualToConstant: cellSize),
collectionView.widthAnchor.constraint(greaterThanOrEqualToConstant: cellSize * 10)
])
//TODO: Look at this width stuff, we should NOT need it!
collectionViewWidth = collectionView.widthAnchor.constraint(equalToConstant: cellSize * 20)
collectionViewWidth?.isActive = true
collectionViewHeight = collectionView.heightAnchor.constraint(equalToConstant: cellSize)
collectionViewHeight?.isActive = true
}
open override func layoutSubviews() {
super.layoutSubviews()
// Accounts for any collection size changes
setHeight()
DispatchQueue.main.async {
self.collectionView.collectionViewLayout.invalidateLayout()
}
}
open func setHeight() {
let swatches = model.selectors
guard swatches.count > 0 else {
collectionViewHeight?.constant = 0
return
}
// Calculate the height
let swatchesInRow = floor(CGFloat(collectionView.bounds.width/(cellSize + itemSpacing)))
let numberOfRows = ceil(CGFloat(swatches.count)/swatchesInRow)
let height = (numberOfRows * cellSize) + (itemSpacing * (numberOfRows-1))
collectionViewHeight?.constant = CGFloat(height)
}
open override func shouldUpdateView(viewModel: ModelType) -> Bool {
return viewModel != model
}