From 5a255aac52ce5a80f7031fab03bb6526d539cb88 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Fri, 16 Aug 2024 15:18:33 -0500 Subject: [PATCH] refactored for bugs in missing buttons for 2 rowCount Signed-off-by: Matt Bruce --- .../ButtonGroupPositionLayout.swift | 81 +++++++++---------- 1 file changed, 38 insertions(+), 43 deletions(-) diff --git a/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift b/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift index 5d3a339f..8c71ac4e 100644 --- a/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift +++ b/VDS/Components/Buttons/ButtonGroup/ButtonGroupPositionLayout.swift @@ -49,52 +49,45 @@ class ButtonCollectionViewRow { func layout(for position: ButtonGroup.Alignment, with collectionViewWidth: CGFloat){ var offset = 0.0 let height = rowHeight + 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 { - - var usedSpace = 0.0 - //get the width for the buttons - for attribute in attributes { - if !attribute.isButton { - usedSpace += attribute.frame.width + 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 } - 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 >= Button.Size.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 - } - } + } else { + for attribute in buttonAttributes { + attribute.frame.size.width = buttonEqualSpacing } } } @@ -181,7 +174,7 @@ class ButtonGroupPositionLayout: UICollectionViewLayout { var rows = [ButtonCollectionViewRow]() rows.append(ButtonCollectionViewRow()) - let collectionViewWidth = collectionView.frame.width + let collectionViewWidth = collectionView.horizontalPinnedWidth() ?? collectionView.frame.width for item in 0.. collectionViewWidth || (rowQuantity > 0 && rowItemCount == rowQuantity) { + if (layoutWidthIterator + itemSize.width) > collectionViewWidth && rowQuantity == 0 + || (rowQuantity > 0 && rowItemCount == rowQuantity) { // If the current row width (after this item being laid out) is exceeding // the width of the collection view content, put it in the next line @@ -318,3 +312,4 @@ class ButtonGroupPositionLayout: UICollectionViewLayout { } } +