Skip to content

Commit 5839d6a

Browse files
committed
FlexLayout - within addItem() remove node from parent if view is moving to a different superview
1 parent bfd1374 commit 5839d6a

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

Sources/Swift/FlexLayout.swift

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,12 @@ public final class Flex {
5858
//
5959

6060
/**
61-
This method adds a flex item (UIView) to a flex container. Internally the methods adds the UIView has subviews and enables flexbox.
61+
Adds a flex item (`UIView`) to the receiver and returns the item's flex interface.
62+
63+
This method internally creates a new `UIView` instance corresponding to the flex item,
64+
and is useful for adding a flex item/container when you don't need to refer to it later.
6265

63-
- Returns: The added view flex interface
66+
- Returns: The flex interface corresponding to the added view.
6467
*/
6568
@discardableResult
6669
public func addItem() -> Flex {
@@ -69,15 +72,23 @@ public final class Flex {
6972
}
7073

7174
/**
72-
This method is similar to `addItem(: UIView)` except that it also creates the flex item's UIView. Internally the method creates an
73-
UIView, adds it has subviews and enables flexbox. This is useful to add a flex item/container easily when you don't need to refer to it later.
75+
Adds a flex item (`UIView`) to the receiver and returns the item's flex interface.
76+
77+
This method enables flexbox for `view` and adds it as a subview of the receiver's associated host view.
7478

75-
- Parameter view: view to add to the flex container
76-
- Returns: The added view flex interface
79+
Just as views can only have one superview, Yoga nodes should only have one parent node. If `view`
80+
already has a superview, this method removes `view`'s associated yoga node as a child from the superview's
81+
associated yoga node.
82+
83+
- Parameter view: The view to be added.
84+
- Returns: The flex interface corresponding to the added view.
7785
*/
7886
@discardableResult
7987
public func addItem(_ view: UIView) -> Flex {
8088
if let host = self.view {
89+
if let superview = view.superview, superview != host && superview.isYogaEnabled {
90+
YGNodeRemoveChild(superview.yoga.node, view.yoga.node)
91+
}
8192
host.addSubview(view)
8293
return view.flex
8394
} else {

0 commit comments

Comments
 (0)