Skip to content

Commit 94efed5

Browse files
authored
Merge pull request #281 from heoblitz/fix/markDirty_error
Fix error when dynamically removing views
2 parents 6b64757 + dc61e3f commit 94efed5

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

Sources/YogaKit/YGLayout.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,13 @@ - (void)markDirty {
227227
// the measure function. Since we already know that this is a leaf,
228228
// this *should* be fine. Forgive me Hack Gods.
229229
const YGNodeRef node = self.node;
230-
if (!YGNodeHasMeasureFunc(node)) {
230+
if (!YGNodeHasMeasureFunc(node) && self.numberOfChildren == 0) {
231231
YGNodeSetMeasureFunc(node, YGMeasureView);
232232
}
233233

234-
YGNodeMarkDirty(node);
234+
if (YGNodeHasMeasureFunc(node)) {
235+
YGNodeMarkDirty(node);
236+
}
235237
}
236238

237239
- (NSUInteger)numberOfChildren {

Tests/FlexLayoutTests/FlexLayoutTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,18 @@ final class FlexLayoutTests: XCTestCase {
2626

2727
XCTAssertNil(weakView, "Creation of flex should not lead to retain cycle")
2828
}
29+
30+
31+
func testRemoveViewDynamically() {
32+
let rootFlexContainer = UIView()
33+
rootFlexContainer.flex.addItem(UIView())
34+
rootFlexContainer.flex.define { _ in }
35+
rootFlexContainer.flex.layout()
36+
37+
rootFlexContainer.subviews.forEach { $0.removeFromSuperview() }
38+
rootFlexContainer.flex.markDirty()
39+
rootFlexContainer.flex.layout()
2940

41+
XCTAssertTrue(rootFlexContainer.subviews.isEmpty)
42+
}
3043
}

0 commit comments

Comments
 (0)