added percentage to button group
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
ae6c380627
commit
62befe1a32
@ -172,7 +172,7 @@ open class Button: ButtonBase, Useable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileprivate extension ButtonSize {
|
internal extension ButtonSize {
|
||||||
|
|
||||||
var height: CGFloat {
|
var height: CGFloat {
|
||||||
switch self {
|
switch self {
|
||||||
|
|||||||
@ -32,6 +32,9 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
|
|||||||
//If provided, width of Button components will be rendered based on this value. If omitted, default button widths are rendered.
|
//If provided, width of Button components will be rendered based on this value. If omitted, default button widths are rendered.
|
||||||
open var buttonWidth: CGFloat? {
|
open var buttonWidth: CGFloat? {
|
||||||
didSet {
|
didSet {
|
||||||
|
if let buttonWidth, let buttonPercentage, buttonWidth > 0, buttonPercentage > 0{
|
||||||
|
self.buttonPercentage = nil
|
||||||
|
}
|
||||||
buttons.forEach { button in
|
buttons.forEach { button in
|
||||||
if let button = button as? Button {
|
if let button = button as? Button {
|
||||||
button.width = buttonWidth
|
button.width = buttonWidth
|
||||||
@ -40,6 +43,23 @@ open class ButtonGroup: View, UICollectionViewDataSource, UICollectionViewDelega
|
|||||||
didChange()
|
didChange()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var _buttonPercentage: CGFloat?
|
||||||
|
open var buttonPercentage: CGFloat? {
|
||||||
|
get { _buttonPercentage }
|
||||||
|
set {
|
||||||
|
if let newValue, newValue <= 100.0, rowQuantity > 0 {
|
||||||
|
_buttonPercentage = newValue
|
||||||
|
} else {
|
||||||
|
_buttonPercentage = nil
|
||||||
|
}
|
||||||
|
if let buttonWidth, let buttonPercentage, buttonWidth > 0, buttonPercentage > 0 {
|
||||||
|
self.buttonWidth = nil
|
||||||
|
}
|
||||||
|
positionLayout.buttonPercentage = buttonPercentage
|
||||||
|
didChange()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Private Properties
|
// MARK: - Private Properties
|
||||||
|
|||||||
@ -21,6 +21,8 @@ class ButtonCollectionViewRow {
|
|||||||
var hasButtons: Bool {
|
var hasButtons: Bool {
|
||||||
attributes.contains(where: { $0.isButton })
|
attributes.contains(where: { $0.isButton })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var buttonPercentage: CGFloat?
|
||||||
|
|
||||||
var rowWidth: CGFloat {
|
var rowWidth: CGFloat {
|
||||||
return attributes.reduce(0, { result, attribute -> CGFloat in
|
return attributes.reduce(0, { result, attribute -> CGFloat in
|
||||||
@ -50,6 +52,54 @@ class ButtonCollectionViewRow {
|
|||||||
let height = rowHeight
|
let height = rowHeight
|
||||||
attributes.last?.spacing = 0
|
attributes.last?.spacing = 0
|
||||||
|
|
||||||
|
//check to see if you have buttons and there is a percentage
|
||||||
|
if let buttonPercentage, hasButtons, buttonPercentage > 0 {
|
||||||
|
|
||||||
|
var usedSpace = 0.0
|
||||||
|
//get the width for the buttons
|
||||||
|
for attribute in attributes {
|
||||||
|
if !attribute.isButton {
|
||||||
|
usedSpace += attribute.frame.width
|
||||||
|
}
|
||||||
|
usedSpace += attribute.spacing
|
||||||
|
}
|
||||||
|
let buttonAvailableSpace = collectionViewWidth - usedSpace
|
||||||
|
let realPercentage = (buttonPercentage / 100)
|
||||||
|
let buttonWidth = realPercentage * buttonAvailableSpace
|
||||||
|
// print("buttonPercentage :\(realPercentage)")
|
||||||
|
// print("collectionView width:\(collectionViewWidth)")
|
||||||
|
// print("usedSpace width:\(usedSpace)")
|
||||||
|
// print("button available width:\(buttonAvailableSpace)")
|
||||||
|
print("each button width:\(buttonWidth)\n")
|
||||||
|
print("minimum widht:\(ButtonSize.large.minimumWidth)")
|
||||||
|
// test sizing
|
||||||
|
var testSize = 0.0
|
||||||
|
var buttonCount = 0.0
|
||||||
|
for attribute in attributes {
|
||||||
|
if attribute.isButton {
|
||||||
|
testSize += buttonWidth
|
||||||
|
buttonCount += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if buttonWidth >= ButtonSize.large.minimumWidth {
|
||||||
|
if testSize <= buttonAvailableSpace {
|
||||||
|
for attribute in attributes {
|
||||||
|
if attribute.isButton {
|
||||||
|
attribute.frame.size.width = buttonWidth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let distributedSize = buttonAvailableSpace / buttonCount
|
||||||
|
for attribute in attributes {
|
||||||
|
if attribute.isButton {
|
||||||
|
attribute.frame.size.width = distributedSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
switch position {
|
switch position {
|
||||||
case .left:
|
case .left:
|
||||||
break
|
break
|
||||||
@ -96,6 +146,7 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
|
|||||||
var layoutHeight: CGFloat = 0.0
|
var layoutHeight: CGFloat = 0.0
|
||||||
var position: ButtonPosition = .left
|
var position: ButtonPosition = .left
|
||||||
var rowQuantity: Int = 0
|
var rowQuantity: Int = 0
|
||||||
|
var buttonPercentage: CGFloat?
|
||||||
|
|
||||||
private var itemCache: [ButtonLayoutAttributes] = []
|
private var itemCache: [ButtonLayoutAttributes] = []
|
||||||
|
|
||||||
@ -205,7 +256,10 @@ class ButtonGroupPositionLayout: UICollectionViewLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// recalculate rows x based off of positions
|
// recalculate rows x based off of positions
|
||||||
rows.forEach { $0.layout(for: position, with: collectionViewWidth) }
|
rows.forEach {
|
||||||
|
$0.buttonPercentage = buttonPercentage
|
||||||
|
$0.layout(for: position, with: collectionViewWidth)
|
||||||
|
}
|
||||||
|
|
||||||
let rowAttributes = rows.flatMap { $0.attributes }
|
let rowAttributes = rows.flatMap { $0.attributes }
|
||||||
itemCache = rowAttributes
|
itemCache = rowAttributes
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user