From 6901f10978d19d0b0a63b2e83eab36a43e568b7a Mon Sep 17 00:00:00 2001 From: Kuluum Date: Fri, 4 Oct 2019 23:33:58 +0300 Subject: [PATCH 1/4] Add vertical, horizontal and horizontalDirected funcs to absolute positioning --- Sources/FlexLayout.swift | 67 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/Sources/FlexLayout.swift b/Sources/FlexLayout.swift index e94e98b1..9ee45c3a 100644 --- a/Sources/FlexLayout.swift +++ b/Sources/FlexLayout.swift @@ -648,6 +648,73 @@ 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 horizontal(_ 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 horizontal(_ 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 start and end edges (LTR=right, RTL=left) 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 horizontalDirected(_ value: CGFloat) -> Flex { + yoga.start = YGValue(value) + yoga.end = YGValue(value) + return self + } + + /** + Set the start and end edges (LTR=right, RTL=left) 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 horizontalDirected(_ percent: FPercent) -> Flex { + yoga.start = YGValue(value: Float(percent.value), unit: .percent) + yoga.end = 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 vertical(_ 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 vertical(_ percent: FPercent) -> Flex { + yoga.top = YGValue(value: Float(percent.value), unit: .percent) + yoga.bottom = YGValue(value: Float(percent.value), unit: .percent) + return self + } + // // MARK: Margins // From 1f0bf3363afae07836af86e325a9e950c60bcfb4 Mon Sep 17 00:00:00 2001 From: Kuluum Date: Tue, 8 Oct 2019 00:36:37 +0300 Subject: [PATCH 2/4] Add readme. Rename vertical -> vertical(ly) --- README.md | 7 ++++++- Sources/FlexLayout.swift | 31 ++++--------------------------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 0df6d70b..e22e12fc 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(), vertical(), vertically(), horizontally() 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,11 @@ 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()`. + 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 9ee45c3a..5f7cbe86 100644 --- a/Sources/FlexLayout.swift +++ b/Sources/FlexLayout.swift @@ -653,7 +653,7 @@ public final class Flex { This method is valid only when the item position is absolute (`view.flex.position(.absolute)`) */ @discardableResult - public func horizontal(_ value: CGFloat) -> Flex { + public func horizontally(_ value: CGFloat) -> Flex { yoga.left = YGValue(value) yoga.right = YGValue(value) return self @@ -664,41 +664,18 @@ public final class Flex { This method is valid only when the item position is absolute (`view.flex.position(.absolute)`) */ @discardableResult - public func horizontal(_ percent: FPercent) -> Flex { + 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 start and end edges (LTR=right, RTL=left) 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 horizontalDirected(_ value: CGFloat) -> Flex { - yoga.start = YGValue(value) - yoga.end = YGValue(value) - return self - } - - /** - Set the start and end edges (LTR=right, RTL=left) 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 horizontalDirected(_ percent: FPercent) -> Flex { - yoga.start = YGValue(value: Float(percent.value), unit: .percent) - yoga.end = 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 vertical(_ value: CGFloat) -> Flex { + public func vertically(_ value: CGFloat) -> Flex { yoga.top = YGValue(value) yoga.bottom = YGValue(value) return self @@ -709,7 +686,7 @@ public final class Flex { This method is valid only when the item position is absolute (`view.flex.position(.absolute)`) */ @discardableResult - public func vertical(_ percent: FPercent) -> Flex { + 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 From d51ee8347cea8492fc3ca43dcf49ba1c7f0ea88d Mon Sep 17 00:00:00 2001 From: Kuluum Date: Tue, 8 Oct 2019 00:39:29 +0300 Subject: [PATCH 3/4] Remove copypaste --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e22e12fc..433d2f7e 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(), vertical(), vertically(), horizontally() +### top(), bottom(), left(), right(), start(), end(), vertically(), horizontally() 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:** From 3ee1593422b56dd5e904d79345ad83f6b47b317a Mon Sep 17 00:00:00 2001 From: Kuluum Date: Tue, 8 Oct 2019 21:11:17 +0300 Subject: [PATCH 4/4] Add all() + some tests --- .../AbsolutionPositionContentSpec.swift | 19 ++++++++++++++ README.md | 5 ++-- Sources/FlexLayout.swift | 26 +++++++++++++++++++ 3 files changed, 48 insertions(+), 2 deletions(-) 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 433d2f7e..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(), 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:** @@ -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. diff --git a/Sources/FlexLayout.swift b/Sources/FlexLayout.swift index 5f7cbe86..df3bcc90 100644 --- a/Sources/FlexLayout.swift +++ b/Sources/FlexLayout.swift @@ -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 //