Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vertical, horizontal #146

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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