diff --git a/FlexLayoutTests/AbsolutionPositionContentSpec.swift b/FlexLayoutTests/AbsolutionPositionContentSpec.swift index 83bd209b..c57551ec 100644 --- a/FlexLayoutTests/AbsolutionPositionContentSpec.swift +++ b/FlexLayoutTests/AbsolutionPositionContentSpec.swift @@ -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))) + } + } } } diff --git a/README.md b/README.md index 0df6d70b..bc45ca44 100644 --- a/README.md +++ b/README.md @@ -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() +### 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:** @@ -797,6 +797,12 @@ Controls the distance a child’s right edge is from the parent’s right edge. Controls the distance a child’s start edge is from the parent’s start edge. In left-to-right direction (LTR), it corresponds to the `left()` property and in RTL to `right()` property. * **`end(: CGFloat)`** / **`end(: FPercent)`**: Controls the distance a child’s end edge is from the parent’s end edge. In left-to-right direction (LTR), it corresponds to the `right()` property and in RTL to `left()` property. +* **`vertically(: CGFloat)`** / **`vertically(: FPercent)`**: +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. diff --git a/Sources/FlexLayout.swift b/Sources/FlexLayout.swift index e94e98b1..df3bcc90 100644 --- a/Sources/FlexLayout.swift +++ b/Sources/FlexLayout.swift @@ -648,6 +648,76 @@ public final class Flex { return self } + /** + Set the left and right 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 horizontally(_ value: CGFloat) -> Flex { + yoga.left = YGValue(value) + yoga.right = YGValue(value) + return self + } + + /** + Set the left and right edges distance from the container edges in percentage of its container width. + This method is valid only when the item position is absolute (`view.flex.position(.absolute)`) + */ + @discardableResult + public func horizontally(_ percent: FPercent) -> Flex { + yoga.left = YGValue(value: Float(percent.value), unit: .percent) + yoga.right = YGValue(value: Float(percent.value), unit: .percent) + return self + } + + /** + Set the top and bottom 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 vertically(_ value: CGFloat) -> Flex { + yoga.top = YGValue(value) + yoga.bottom = YGValue(value) + return self + } + + /** + Set the top and bottom edges distance from the container edges in percentage of its container height. + This method is valid only when the item position is absolute (`view.flex.position(.absolute)`) + */ + @discardableResult + public func vertically(_ percent: FPercent) -> Flex { + yoga.top = YGValue(value: Float(percent.value), unit: .percent) + yoga.bottom = YGValue(value: Float(percent.value), unit: .percent) + 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 //