Skip to content

Commit d38dd60

Browse files
authored
Merge pull request #146 from Kuluum/additional_absolute_pos
Add vertical, horizontal and horizontalDirected
2 parents 21921a7 + 3ee1593 commit d38dd60

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

FlexLayoutTests/AbsolutionPositionContentSpec.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ class AbsolutionPositionContentSpec: QuickSpec {
5858
rootFlexContainer.flex.layout()
5959
expect(aView.frame).to(equal(CGRect(x: 0.0, y: 0.0, width: 300.0, height: 200.0)))
6060
}
61+
62+
it("position(.absolute)") {
63+
rootFlexContainer.flex.define { (flex) in
64+
flex.addItem(aView).position(.absolute).horizontally(15).vertically(20)
65+
}
66+
67+
rootFlexContainer.flex.layout()
68+
expect(aView.frame).to(equal(CGRect(x: 15.0, y: 20.0, width: 370.0, height: 360.0)))
69+
}
70+
71+
it("position(.absolute)") {
72+
rootFlexContainer.flex.define { (flex) in
73+
flex.addItem(aView).position(.absolute).all(45)
74+
}
75+
76+
rootFlexContainer.flex.layout()
77+
expect(aView.frame).to(equal(CGRect(x: 45.0, y: 45.0, width: 310.0, height: 310.0)))
78+
}
79+
6180
}
6281
}
6382
}

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ The position property tells Flexbox how you want your item to be positioned with
780780
view.flex.position(.absolute).top(10).left(10).size(50)
781781
```
782782

783-
### top(), bottom(), left(), right(), start(), end()
783+
### top(), bottom(), left(), right(), start(), end(), vertically(), horizontally(), all()
784784
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:
785785

786786
**Methods:**
@@ -797,6 +797,12 @@ Controls the distance a child’s right edge is from the parent’s right edge.
797797
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.
798798
* **`end(: CGFloat)`** / **`end(: FPercent)`**:
799799
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.
800+
* **`vertically(: CGFloat)`** / **`vertically(: FPercent)`**:
801+
Controls the distance child’s top and bottom edges from the parent’s edges. Equal to `top().bottom()`.
802+
* **`horizontally(: CGFloat)`** / **`horizontally(: FPercent)`**:
803+
Controls the distance child’s left and right edges from the parent’s edges. Equal to `left().right()`.
804+
* **`all(: CGFloat)`** / **`all(: FPercent)`**:
805+
Controls the distance child’s edges from the parent’s edges. Equal to `top().bottom().left().right()`.
800806

801807
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.
802808

Sources/FlexLayout.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,76 @@ public final class Flex {
648648
return self
649649
}
650650

651+
/**
652+
Set the left and right edges distance from the container edges in pixels.
653+
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
654+
*/
655+
@discardableResult
656+
public func horizontally(_ value: CGFloat) -> Flex {
657+
yoga.left = YGValue(value)
658+
yoga.right = YGValue(value)
659+
return self
660+
}
661+
662+
/**
663+
Set the left and right edges distance from the container edges in percentage of its container width.
664+
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
665+
*/
666+
@discardableResult
667+
public func horizontally(_ percent: FPercent) -> Flex {
668+
yoga.left = YGValue(value: Float(percent.value), unit: .percent)
669+
yoga.right = YGValue(value: Float(percent.value), unit: .percent)
670+
return self
671+
}
672+
673+
/**
674+
Set the top and bottom edges distance from the container edges in pixels.
675+
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
676+
*/
677+
@discardableResult
678+
public func vertically(_ value: CGFloat) -> Flex {
679+
yoga.top = YGValue(value)
680+
yoga.bottom = YGValue(value)
681+
return self
682+
}
683+
684+
/**
685+
Set the top and bottom edges distance from the container edges in percentage of its container height.
686+
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
687+
*/
688+
@discardableResult
689+
public func vertically(_ percent: FPercent) -> Flex {
690+
yoga.top = YGValue(value: Float(percent.value), unit: .percent)
691+
yoga.bottom = YGValue(value: Float(percent.value), unit: .percent)
692+
return self
693+
}
694+
695+
/**
696+
Set all edges distance from the container edges in pixels.
697+
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
698+
*/
699+
@discardableResult
700+
public func all(_ value: CGFloat) -> Flex {
701+
yoga.top = YGValue(value)
702+
yoga.left = YGValue(value)
703+
yoga.bottom = YGValue(value)
704+
yoga.right = YGValue(value)
705+
return self
706+
}
707+
708+
/**
709+
Set all edges distance from the container edges in percentage of its container size.
710+
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
711+
*/
712+
@discardableResult
713+
public func all(_ percent: FPercent) -> Flex {
714+
yoga.top = YGValue(value: Float(percent.value), unit: .percent)
715+
yoga.left = YGValue(value: Float(percent.value), unit: .percent)
716+
yoga.bottom = YGValue(value: Float(percent.value), unit: .percent)
717+
yoga.right = YGValue(value: Float(percent.value), unit: .percent)
718+
return self
719+
}
720+
651721
//
652722
// MARK: Margins
653723
//

0 commit comments

Comments
 (0)