updated width/height for collectionview
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
61c5d5adf8
commit
c05f35de03
@ -62,8 +62,12 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
public var label = Label()
|
public var label = Label()
|
||||||
private let cellSize: CGFloat = 48.0
|
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 lineSpacing: CGFloat = 12.0
|
||||||
private let itemSpacing: CGFloat = 16.0
|
private let itemSpacing: CGFloat = 16.0
|
||||||
|
private var collectionViewHeight: NSLayoutConstraint?
|
||||||
|
private var collectionViewWidth: NSLayoutConstraint?
|
||||||
|
|
||||||
private lazy var collectionView: UICollectionView = {
|
private lazy var collectionView: UICollectionView = {
|
||||||
let layout = UICollectionViewFlowLayout().with {
|
let layout = UICollectionViewFlowLayout().with {
|
||||||
@ -74,6 +78,7 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
|
|||||||
$0.backgroundColor = .clear
|
$0.backgroundColor = .clear
|
||||||
$0.showsHorizontalScrollIndicator = false
|
$0.showsHorizontalScrollIndicator = false
|
||||||
$0.showsVerticalScrollIndicator = false
|
$0.showsVerticalScrollIndicator = false
|
||||||
|
$0.isScrollEnabled = false
|
||||||
$0.translatesAutoresizingMaskIntoConstraints = false
|
$0.translatesAutoresizingMaskIntoConstraints = false
|
||||||
$0.register(CollectionViewCell<ModelHandlerType>.self, forCellWithReuseIdentifier: "collectionViewCell")
|
$0.register(CollectionViewCell<ModelHandlerType>.self, forCellWithReuseIdentifier: "collectionViewCell")
|
||||||
}
|
}
|
||||||
@ -105,13 +110,43 @@ public class RadioSwatchGroupBase<GroupModelType: RadioSwatchGroupModel, ModelHa
|
|||||||
label.topAnchor.constraint(equalTo: topAnchor),
|
label.topAnchor.constraint(equalTo: topAnchor),
|
||||||
label.leadingAnchor.constraint(equalTo: leadingAnchor),
|
label.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||||
label.trailingAnchor.constraint(equalTo: trailingAnchor),
|
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.leadingAnchor.constraint(equalTo: leadingAnchor),
|
||||||
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
collectionView.trailingAnchor.constraint(equalTo: trailingAnchor),
|
||||||
collectionView.bottomAnchor.constraint(equalTo: bottomAnchor),
|
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 {
|
open override func shouldUpdateView(viewModel: ModelType) -> Bool {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user