From 0979e02ab7310282e65f51cc57a06596e72e33d2 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Sat, 13 Mar 2021 20:26:19 +0400 Subject: [PATCH] Fix segmented control sizeToFit layout --- .../Sources/SegmentedControlNode.swift | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/submodules/SegmentedControlNode/Sources/SegmentedControlNode.swift b/submodules/SegmentedControlNode/Sources/SegmentedControlNode.swift index a3a33b942a..87ae41801a 100644 --- a/submodules/SegmentedControlNode/Sources/SegmentedControlNode.swift +++ b/submodules/SegmentedControlNode/Sources/SegmentedControlNode.swift @@ -290,9 +290,7 @@ public final class SegmentedControlNode: ASDisplayNode, UIGestureRecognizerDeleg public func updateLayout(_ layout: SegmentedControlLayout, transition: ContainedViewLayoutTransition) -> CGSize { self.validLayout = layout - - let calculatedWidth: CGFloat = 0.0 - + let width: CGFloat let height: CGFloat switch layout { @@ -300,6 +298,14 @@ public final class SegmentedControlNode: ASDisplayNode, UIGestureRecognizerDeleg width = targetWidth height = 32.0 case let .sizeToFit(maximumWidth, minimumWidth, targetHeight): + var calculatedWidth: CGFloat = 0.0 + var maxWidth: CGFloat = 0.0 + for item in self.itemNodes { + let size = item.calculateSizeThatFits(CGSize(width: maximumWidth, height: targetHeight)) + maxWidth = max(maxWidth, size.width) + } + calculatedWidth = ceil(maxWidth * CGFloat(self.itemNodes.count)) + width = max(minimumWidth, min(maximumWidth, calculatedWidth)) height = targetHeight }