Skip to content

Commit

Permalink
Add all() + some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuluum committed Oct 8, 2019
1 parent d51ee83 commit 3ee1593
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
19 changes: 19 additions & 0 deletions FlexLayoutTests/AbsolutionPositionContentSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ class AbsolutionPositionContentSpec: QuickSpec {
rootFlexContainer.flex.layout()
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 0.0, width: 300.0, height: 200.0)))
}

it("position(.absolute)") {
rootFlexContainer.flex.define { (flex) in
flex.addItem(aView).position(.absolute).horizontally(15).vertically(20)
}

rootFlexContainer.flex.layout()
expect(aView.frame).to(equal(CGRect(x: 15.0, y: 20.0, width: 370.0, height: 360.0)))
}

it("position(.absolute)") {
rootFlexContainer.flex.define { (flex) in
flex.addItem(aView).position(.absolute).all(45)
}

rootFlexContainer.flex.layout()
expect(aView.frame).to(equal(CGRect(x: 45.0, y: 45.0, width: 310.0, height: 310.0)))
}

}
}
}
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ The position property tells Flexbox how you want your item to be positioned with
view.flex.position(.absolute).top(10).left(10).size(50)
```

### top(), bottom(), left(), right(), start(), end(), vertically(), horizontally()
### top(), bottom(), left(), right(), start(), end(), vertically(), horizontally(), all()
A flex item which is `position` is set to `.absolute` is positioned absolutely in regards to its parent. This is done through the following methods:

**Methods:**
Expand All @@ -801,7 +801,8 @@ Controls the distance a child’s end edge is from the parent’s end edge. In l
Controls the distance child’s top and bottom edges from the parent’s edges. Equal to `top().bottom()`.
* **`horizontally(: CGFloat)`** / **`horizontally(: FPercent)`**:
Controls the distance child’s left and right edges from the parent’s edges. Equal to `left().right()`.

* **`all(: CGFloat)`** / **`all(: FPercent)`**:
Controls the distance child’s edges from the parent’s edges. Equal to `top().bottom().left().right()`.

Using these properties you can control the size and position of an absolute item within its parent. Because absolutely positioned children don’t affect their sibling's layout. Absolute position can be used to create overlays and stack children in the Z axis.

Expand Down
26 changes: 26 additions & 0 deletions Sources/FlexLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,32 @@ public final class Flex {
return self
}

/**
Set all edges distance from the container edges in pixels.
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
*/
@discardableResult
public func all(_ value: CGFloat) -> Flex {
yoga.top = YGValue(value)
yoga.left = YGValue(value)
yoga.bottom = YGValue(value)
yoga.right = YGValue(value)
return self
}

/**
Set all edges distance from the container edges in percentage of its container size.
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
*/
@discardableResult
public func all(_ percent: FPercent) -> Flex {
yoga.top = YGValue(value: Float(percent.value), unit: .percent)
yoga.left = YGValue(value: Float(percent.value), unit: .percent)
yoga.bottom = YGValue(value: Float(percent.value), unit: .percent)
yoga.right = YGValue(value: Float(percent.value), unit: .percent)
return self
}

//
// MARK: Margins
//
Expand Down

0 comments on commit 3ee1593

Please sign in to comment.