Skip to content

Commit 829a2e1

Browse files
authored
Account for insets when applying orthogonal alignment in view distribution (#125)
1 parent 09a4877 commit 829a2e1

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

Example/ParalayoutSnapshotTests/ViewDistributionSnapshotTests.swift

+74
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,80 @@ final class ViewDistributionSnapshotTests: SnapshotTestCase {
123123
)
124124
}
125125

126+
@MainActor
127+
func testHorizontalDistributionAlongBaseline() {
128+
final class TestView: UIView {
129+
130+
// MARK: Initialization
131+
132+
override init(frame: CGRect) {
133+
super.init(frame: frame)
134+
135+
referenceView.backgroundColor = .black.withAlphaComponent(0.1)
136+
addSubview(referenceView)
137+
138+
label1.font = .systemFont(ofSize: 12)
139+
label1.text = "Hello worldy"
140+
label1.textColor = .black
141+
addSubview(label1)
142+
143+
label2.font = .systemFont(ofSize: 16)
144+
label2.text = "Hello worldy"
145+
label2.textColor = .black
146+
addSubview(label2)
147+
148+
label3.font = .boldSystemFont(ofSize: 14)
149+
label3.text = "Hello worldy"
150+
label3.textColor = .black
151+
addSubview(label3)
152+
153+
backgroundColor = .white
154+
}
155+
156+
@available(*, unavailable)
157+
required init?(coder: NSCoder) {
158+
fatalError("init(coder:) has not been implemented")
159+
}
160+
161+
// MARK: Private
162+
163+
private let label1: UILabel = .init()
164+
165+
private let label2: UILabel = .init()
166+
167+
private let label3: UILabel = .init()
168+
169+
private let referenceView: UIView = .init()
170+
171+
// MARK: UIView
172+
173+
override func layoutSubviews() {
174+
label1.sizeToFit()
175+
label2.sizeToFit()
176+
label3.sizeToFit()
177+
178+
applyHorizontalSubviewDistribution(
179+
[
180+
label1.distributionItemUsingCapInsets,
181+
label2.distributionItemUsingCapInsets,
182+
label3.distributionItemUsingCapInsets,
183+
],
184+
inRect: bounds.insetBy(bottom: 8),
185+
orthogonalAlignment: .bottom(inset: 0)
186+
)
187+
188+
referenceView.untransformedFrame = bounds.slice(from: .maxYEdge, amount: 8).slice
189+
}
190+
191+
}
192+
193+
assertSnapshot(
194+
matching: TestView(frame: .init(x: 0, y: 0, width: 400, height: 64)),
195+
as: .image,
196+
named: nameForSnapshot(with: [])
197+
)
198+
}
199+
126200
}
127201

128202
// MARK: -

Paralayout/UIView+Distribution.swift

+2
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,9 @@ extension UIView {
478478
fatalError("Unknown user interface layout direction")
479479
}
480480

481+
frame = frame.inset(by: insets)
481482
applyOrthogonalAlignment(&frame, layoutBounds)
483+
frame = frame.outset(by: insets)
482484

483485
subview.untransformedFrame = frame
484486

0 commit comments

Comments
 (0)