refactored more code
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
5a255aac52
commit
7efc0096af
@ -49,49 +49,54 @@ class ButtonCollectionViewRow {
|
||||
func layout(for position: ButtonGroup.Alignment, with collectionViewWidth: CGFloat){
|
||||
var offset = 0.0
|
||||
let height = rowHeight
|
||||
|
||||
attributes.last?.spacing = 0
|
||||
|
||||
//filter only the buttons since this is the only
|
||||
//object we can change the frames for.
|
||||
let buttonAttributes = attributes.filter{$0.isButton}
|
||||
|
||||
attributes.last?.spacing = 0
|
||||
|
||||
///Calculate the available space in the row
|
||||
let totalSpacingBetweenAttributes = attributes.reduce(0.0) { $0 + $1.spacing }
|
||||
let availableWidthAfterSpacing = collectionViewWidth - totalSpacingBetweenAttributes
|
||||
let buttonCount = CGFloat(buttonAttributes.count)
|
||||
|
||||
//see how much of the rows width is used for
|
||||
//non-buttons that are BaseButton Subclasses that are not "Button"
|
||||
let nonButtonSpace = attributes.filter { !$0.isButton }.reduce(0.0) { $0 + $1.frame.width + $1.spacing }
|
||||
let maxButtonWidth = availableWidthAfterSpacing / buttonCount
|
||||
|
||||
//getting available button space since textlinks need their space
|
||||
let buttonsAvailableSpace = collectionViewWidth - nonButtonSpace
|
||||
var buttonWidth = maxButtonWidth
|
||||
var buttonCalculatedPercentage: CGFloat = 0.0
|
||||
let buttonEqualSpacing = buttonsAvailableSpace / buttonCount
|
||||
|
||||
// test sizing
|
||||
let testSize = buttonWidth * CGFloat(buttonAttributes.count)
|
||||
|
||||
//check to see if you have buttons and there is a percentage
|
||||
if let buttonPercentage, hasButtons, buttonPercentage > 0 {
|
||||
buttonCalculatedPercentage = CGFloat(buttonPercentage / 100.0)
|
||||
let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace
|
||||
buttonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth)
|
||||
}
|
||||
|
||||
//resize the buttonAttributes
|
||||
if buttonWidth >= Button.Size.large.minimumWidth && !buttonAttributes.isEmpty {
|
||||
if testSize <= buttonsAvailableSpace {
|
||||
for attribute in buttonAttributes {
|
||||
attribute.frame.size.width = buttonCalculatedPercentage.isZero ? min(attribute.frame.size.width, buttonWidth) : buttonWidth
|
||||
}
|
||||
} else {
|
||||
for attribute in buttonAttributes {
|
||||
attribute.frame.size.width = buttonEqualSpacing
|
||||
if !buttonAttributes.isEmpty {
|
||||
let buttonCount = CGFloat(buttonAttributes.count)
|
||||
|
||||
///Calculate the spaces between items in the row
|
||||
let totalSpacingBetweenAttributes = attributes.reduce(0.0) { $0 + $1.spacing }
|
||||
|
||||
//see how much of the rows width is used for
|
||||
//non-buttons that are BaseButton Subclasses that are not "Button"
|
||||
let nonButtonSpace = attributes.filter { !$0.isButton }.reduce(0.0) { $0 + $1.frame.width + $1.spacing }
|
||||
|
||||
//getting available button space since textlinks need their space
|
||||
let buttonsAvailableSpace = collectionViewWidth - nonButtonSpace - totalSpacingBetweenAttributes
|
||||
var maxButtonWidth = buttonsAvailableSpace / buttonCount
|
||||
var buttonCalculatedPercentage: CGFloat = 0.0
|
||||
let buttonEqualSpacing = buttonsAvailableSpace / buttonCount
|
||||
|
||||
// test sizing
|
||||
let testSize = maxButtonWidth * CGFloat(buttonAttributes.count)
|
||||
|
||||
//check to see if you have buttons and there is a percentage
|
||||
if let buttonPercentage, hasButtons, buttonPercentage > 0 {
|
||||
buttonCalculatedPercentage = CGFloat(buttonPercentage / 100.0)
|
||||
let buttonPercentageWidth = buttonCalculatedPercentage * buttonsAvailableSpace
|
||||
maxButtonWidth = min(max(buttonPercentageWidth, Button.Size.large.minimumWidth), maxButtonWidth)
|
||||
}
|
||||
|
||||
//resize the buttonAttributes
|
||||
if maxButtonWidth >= Button.Size.large.minimumWidth && !buttonAttributes.isEmpty {
|
||||
if testSize <= buttonsAvailableSpace {
|
||||
for attribute in buttonAttributes {
|
||||
attribute.frame.size.width = buttonCalculatedPercentage.isZero ? min(attribute.frame.size.width, maxButtonWidth) : maxButtonWidth
|
||||
}
|
||||
} else {
|
||||
for attribute in buttonAttributes {
|
||||
attribute.frame.size.width = buttonEqualSpacing
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//update the offset based on position
|
||||
switch position {
|
||||
case .left:
|
||||
break
|
||||
@ -313,3 +318,4 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user