Skip to content

Commit 09a4877

Browse files
authored
Correctly account for zero-width spacers in implied distribution (#127)
Currently the implied distribution does not behave as documented in cases where the fixed spacers sum to zero (either by using zero-size spacers or by combining positive- and negative-sized spacers). This updates the logic to correctly identify the use of _any_ fixed spacer. Resolves #126
1 parent f426143 commit 09a4877

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Paralayout/ViewDistributionItem.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public enum ViewDistributionItem: ViewDistributionSpecifying, Sendable {
6666
var distributionItems = [ViewDistributionItem]()
6767
var totalViewSize: CGFloat = 0
6868
var totalFixedSpace: CGFloat = 0
69+
var hasFixedSpacers: Bool = false
6970
var totalFlexibleSpace: CGFloat = 0
7071

7172
var subviewsToDistribute = Set<UIView>()
@@ -92,6 +93,7 @@ public enum ViewDistributionItem: ViewDistributionSpecifying, Sendable {
9293

9394
case .fixed:
9495
totalFixedSpace += layoutSize
96+
hasFixedSpacers = true
9597

9698
case .flexible:
9799
totalFlexibleSpace += layoutSize
@@ -107,7 +109,7 @@ public enum ViewDistributionItem: ViewDistributionSpecifying, Sendable {
107109

108110
// Insert flexible space if necessary.
109111
if totalFlexibleSpace == 0 {
110-
if totalFixedSpace == 0 {
112+
if !hasFixedSpacers {
111113
// No spacers at all: insert `1.flexible` between all items.
112114
for i in 0 ..< (distributionItems.count + 1) {
113115
distributionItems.insert(1.flexible, at: i * 2)

ParalayoutTests/DistributionTests.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,26 @@ final class DistributionTests: XCTestCase {
214214
}
215215
}
216216

217+
// MARK: - Tests - Spacers
218+
219+
@MainActor
220+
func testZeroSizeFixedSpacer() {
221+
let container = UIView(frame: .init(x: 0, y: 0, width: 100, height: 400))
222+
container.semanticContentAttribute = .forceRightToLeft
223+
224+
let firstSubview = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
225+
container.addSubview(firstSubview)
226+
let secondSubview = UIView(frame: .init(x: 0, y: 0, width: 100, height: 100))
227+
container.addSubview(secondSubview)
228+
229+
container.applyVerticalSubviewDistribution {
230+
firstSubview
231+
0.fixed
232+
secondSubview
233+
}
234+
235+
XCTAssertEqual(firstSubview.frame, .init(x: 0, y: 100, width: 100, height: 100))
236+
XCTAssertEqual(secondSubview.frame, .init(x: 0, y: 200, width: 100, height: 100))
237+
}
238+
217239
}

0 commit comments

Comments
 (0)