Skip to content

Commit

Permalink
Merge pull request #146 from Kuluum/additional_absolute_pos
Browse files Browse the repository at this point in the history
Add vertical, horizontal and horizontalDirected
  • Loading branch information
lucdion authored Oct 9, 2019
2 parents 21921a7 + 3ee1593 commit d38dd60
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
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)))
}

}
}
}
8 changes: 7 additions & 1 deletion 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()
### 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 @@ -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.

Expand Down
70 changes: 70 additions & 0 deletions Sources/FlexLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
//
Expand Down

0 comments on commit d38dd60

Please sign in to comment.