Skip to content

Commit eb8ee81

Browse files
authored
Merge pull request #254 from heoblitz/feature/add_static_position
Add static position feature
2 parents 40e4217 + b52c200 commit eb8ee81

File tree

8 files changed

+262
-115
lines changed

8 files changed

+262
-115
lines changed

FlexLayout.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
24AE97411FEAF30A00995987 /* UIView+FlexLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24AE97321FEAF30A00995987 /* UIView+FlexLayout.swift */; };
1515
24AE97421FEAF30A00995987 /* Percent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24AE97331FEAF30A00995987 /* Percent.swift */; };
1616
24AE974A1FEAF30A00995987 /* YGLayoutExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24AE973D1FEAF30A00995987 /* YGLayoutExtensions.swift */; };
17-
711F2EE62ACC727000DDDD67 /* AbsolutionPositionContentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711F2EE02ACC727000DDDD67 /* AbsolutionPositionContentTests.swift */; };
1817
711F2EE72ACC727000DDDD67 /* WidthSizeContentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711F2EE12ACC727000DDDD67 /* WidthSizeContentTests.swift */; };
1918
711F2EE82ACC727000DDDD67 /* PaddingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711F2EE22ACC727000DDDD67 /* PaddingTests.swift */; };
2019
711F2EE92ACC727000DDDD67 /* FlexLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711F2EE32ACC727000DDDD67 /* FlexLayoutTests.swift */; };
@@ -99,6 +98,7 @@
9998
EF0E61762C1A811100DF30F6 /* YGNodeStyle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EF0E612C2C1A811100DF30F6 /* YGNodeStyle.cpp */; };
10099
EF0E61772C1A811100DF30F6 /* YGPixelGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EF0E612D2C1A811100DF30F6 /* YGPixelGrid.cpp */; };
101100
EF0E61782C1A811100DF30F6 /* YGValue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EF0E612E2C1A811100DF30F6 /* YGValue.cpp */; };
101+
EF5445402C1D607100BF4A33 /* InsetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF54453F2C1D607100BF4A33 /* InsetTests.swift */; };
102102
/* End PBXBuildFile section */
103103

104104
/* Begin PBXContainerItemProxy section */
@@ -138,7 +138,6 @@
138138
24DA37631EF843C500D1AB2F /* FlexLayoutTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FlexLayoutTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
139139
2B210ECFE9D7665DACB8E8A7 /* Pods-FlexLayoutTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FlexLayoutTests.debug.xcconfig"; path = "Target Support Files/Pods-FlexLayoutTests/Pods-FlexLayoutTests.debug.xcconfig"; sourceTree = "<group>"; };
140140
38E690AA4AF8EDFC95DB6626 /* Pods-FlexLayoutTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FlexLayoutTests.release.xcconfig"; path = "Target Support Files/Pods-FlexLayoutTests/Pods-FlexLayoutTests.release.xcconfig"; sourceTree = "<group>"; };
141-
711F2EE02ACC727000DDDD67 /* AbsolutionPositionContentTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AbsolutionPositionContentTests.swift; sourceTree = "<group>"; };
142141
711F2EE12ACC727000DDDD67 /* WidthSizeContentTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WidthSizeContentTests.swift; sourceTree = "<group>"; };
143142
711F2EE22ACC727000DDDD67 /* PaddingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaddingTests.swift; sourceTree = "<group>"; };
144143
711F2EE32ACC727000DDDD67 /* FlexLayoutTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlexLayoutTests.swift; sourceTree = "<group>"; };
@@ -223,6 +222,7 @@
223222
EF0E612C2C1A811100DF30F6 /* YGNodeStyle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGNodeStyle.cpp; sourceTree = "<group>"; };
224223
EF0E612D2C1A811100DF30F6 /* YGPixelGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGPixelGrid.cpp; sourceTree = "<group>"; };
225224
EF0E612E2C1A811100DF30F6 /* YGValue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGValue.cpp; sourceTree = "<group>"; };
225+
EF54453F2C1D607100BF4A33 /* InsetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InsetTests.swift; sourceTree = "<group>"; };
226226
/* End PBXFileReference section */
227227

228228
/* Begin PBXFrameworksBuildPhase section */
@@ -304,12 +304,12 @@
304304
711F2EDF2ACC727000DDDD67 /* FlexLayoutTests */ = {
305305
isa = PBXGroup;
306306
children = (
307-
711F2EE02ACC727000DDDD67 /* AbsolutionPositionContentTests.swift */,
308307
711F2EE12ACC727000DDDD67 /* WidthSizeContentTests.swift */,
309308
711F2EE22ACC727000DDDD67 /* PaddingTests.swift */,
310309
711F2EE32ACC727000DDDD67 /* FlexLayoutTests.swift */,
311310
711F2EE42ACC727000DDDD67 /* MarginTests.swift */,
312311
711F2EE52ACC727000DDDD67 /* JustifyContentTests.swift */,
312+
EF54453F2C1D607100BF4A33 /* InsetTests.swift */,
313313
);
314314
path = FlexLayoutTests;
315315
sourceTree = "<group>";
@@ -715,8 +715,8 @@
715715
711F2EEA2ACC727000DDDD67 /* MarginTests.swift in Sources */,
716716
711F2EE92ACC727000DDDD67 /* FlexLayoutTests.swift in Sources */,
717717
711F2EE82ACC727000DDDD67 /* PaddingTests.swift in Sources */,
718-
711F2EE62ACC727000DDDD67 /* AbsolutionPositionContentTests.swift in Sources */,
719718
711F2EEB2ACC727000DDDD67 /* JustifyContentTests.swift in Sources */,
719+
EF5445402C1D607100BF4A33 /* InsetTests.swift in Sources */,
720720
711F2EE72ACC727000DDDD67 /* WidthSizeContentTests.swift in Sources */,
721721
);
722722
runOnlyForDeploymentPostprocessing = 0;

Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PODS:
2-
- FlexLayout (2.0.7)
2+
- FlexLayout (2.0.8)
33
- PinLayout (1.10.5)
44
- SwiftLint (0.55.1)
55

@@ -18,7 +18,7 @@ EXTERNAL SOURCES:
1818
:path: "./"
1919

2020
SPEC CHECKSUMS:
21-
FlexLayout: fcb9aaa754029afff0cb99e9cdfe10c68d70b6ac
21+
FlexLayout: 74c80db7698ea6e0d11e00ba012c0b9f3252938a
2222
PinLayout: f6c2b63a5a5b24864064e1d15c67de41b4e74748
2323
SwiftLint: 3fe909719babe5537c552ee8181c0031392be933
2424

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Flexbox is an incredible improvement over UIStackView. It is simpler to use, muc
3636
* [Creation, modification and definition of flexbox containers](#create_modify_define_containers)
3737
* [Flexbox containers properties](#containers_properties)
3838
* [Flexbox items properties](#intems_properties)
39-
* [Absolute positioning](#absolute_positioning)
39+
* [Positioning](#positioning)
4040
* [Adjusting the size](#adjusting_size)
4141
* [Width, height and size](#width_height_size)
4242
* [minWidth, maxWidth, minHeight, maxHeight](#minmax_width_height_size)
@@ -778,25 +778,32 @@ Item natural size, considering only properties of the view itself. Independent o
778778

779779
<br>
780780

781-
<a name="absolute_positioning"></a>
782-
## 4. Absolute positioning
781+
<a name="positioning"></a>
782+
## 4. Positioning
783783
- Applies to: `flex items`
784784
- Parameter: CGFloat
785785

786786
**Method:**
787787

788788
* **`position(_: Position)`**
789789
The position property tells Flexbox how you want your item to be positioned within its parent. Position values:
790-
* **`relative (default)`**
791-
* **`absolute`**: The view is positioned using properties: top(), bottom(), left(), right(), start(), end().
790+
* **`relative (default)`** The view is positioned using Insets properties (top, bottom, left, right, start, end) from its normal position within its flex container and will take up space within the flex container. This node will always form a containing block.
791+
* **`absolute`**: The view is removed from the flex container's flow and is positioned using Insets properties (top, bottom, left, right, start, end). Insets will offset the node from its containing block.
792+
* **`static`**: The view ignores insets and will not form a containing block.
792793

793794
###### Usage examples:
794795
```swift
796+
view.flex.position(.relative).top(10).left(10).size(50)
797+
795798
view.flex.position(.absolute).top(10).left(10).size(50)
796799
```
797800

798801
### top(), bottom(), left(), right(), start(), end(), vertically(), horizontally(), all()
799-
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:
802+
A flex item with position set to .relative is positioned relative to its normal position within its flex container. Only one property can be applied in each direction, either vertical or horizontal. If both `top` and `bottom` properties are set, `top` takes precedence. If both `left` and `right` properties are set, `left` takes precedence.
803+
804+
A flex item with position set to .absolute is positioned absolutely with respect to its containing block. 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.
805+
806+
This is done through the following methods:
800807

801808
**Methods:**
802809

@@ -819,10 +826,10 @@ Controls the distance child’s left and right edges from the parent’s edges.
819826
* **`all(: CGFloat)`** / **`all(: FPercent)`**:
820827
Controls the distance child’s edges from the parent’s edges. Equal to `top().bottom().left().right()`.
821828

822-
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.
823-
824829
###### Usage examples:
825830
```swift
831+
view.flex.position(.relative).top(10).right(10).width(100).height(50)
832+
826833
view.flex.position(.absolute).top(10).right(10).width(100).height(50)
827834
view.flex.position(.absolute).left(20%).right(20%)
828835
```

Sources/Swift/FlexLayout.swift

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ public final class Flex {
545545
}
546546

547547
//
548-
// MARK: Absolute positionning
548+
// MARK: Position / Inset
549549
//
550550

551551
/**
@@ -561,7 +561,7 @@ public final class Flex {
561561

562562
/**
563563
Set the left edge distance from the container left edge in pixels.
564-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
564+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
565565
*/
566566
@discardableResult
567567
public func left(_ value: CGFloat) -> Flex {
@@ -571,7 +571,7 @@ public final class Flex {
571571

572572
/**
573573
Set the left edge distance from the container left edge in percentage of its container width.
574-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
574+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
575575
*/
576576
@discardableResult
577577
public func left(_ percent: FPercent) -> Flex {
@@ -581,7 +581,7 @@ public final class Flex {
581581

582582
/**
583583
Set the top edge distance from the container top edge in pixels.
584-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
584+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
585585
*/
586586
@discardableResult
587587
public func top(_ value: CGFloat) -> Flex {
@@ -591,7 +591,7 @@ public final class Flex {
591591

592592
/**
593593
Set the top edge distance from the container top edge in percentage of its container height.
594-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
594+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
595595
*/
596596
@discardableResult
597597
public func top(_ percent: FPercent) -> Flex {
@@ -601,7 +601,7 @@ public final class Flex {
601601

602602
/**
603603
Set the right edge distance from the container right edge in pixels.
604-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
604+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
605605
*/
606606
@discardableResult
607607
public func right(_ value: CGFloat) -> Flex {
@@ -611,7 +611,7 @@ public final class Flex {
611611

612612
/**
613613
Set the right edge distance from the container right edge in percentage of its container width.
614-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
614+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
615615
*/
616616
@discardableResult
617617
public func right(_ percent: FPercent) -> Flex {
@@ -621,7 +621,7 @@ public final class Flex {
621621

622622
/**
623623
Set the bottom edge distance from the container bottom edge in pixels.
624-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
624+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
625625
*/
626626
@discardableResult
627627
public func bottom(_ value: CGFloat) -> Flex {
@@ -631,7 +631,7 @@ public final class Flex {
631631

632632
/**
633633
Set the bottom edge distance from the container bottom edge in percentage of its container height.
634-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
634+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
635635
*/
636636
@discardableResult
637637
public func bottom(_ percent: FPercent) -> Flex {
@@ -641,7 +641,7 @@ public final class Flex {
641641

642642
/**
643643
Set the start edge (LTR=left, RTL=right) distance from the container start edge in pixels.
644-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
644+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
645645
*/
646646
@discardableResult
647647
public func start(_ value: CGFloat) -> Flex {
@@ -652,7 +652,7 @@ public final class Flex {
652652
/**
653653
Set the start edge (LTR=left, RTL=right) distance from the container start edge in
654654
percentage of its container width.
655-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
655+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
656656
*/
657657
@discardableResult
658658
public func start(_ percent: FPercent) -> Flex {
@@ -662,7 +662,7 @@ public final class Flex {
662662

663663
/**
664664
Set the end edge (LTR=right, RTL=left) distance from the container end edge in pixels.
665-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
665+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
666666
*/
667667
@discardableResult
668668
public func end(_ value: CGFloat) -> Flex {
@@ -673,7 +673,7 @@ public final class Flex {
673673
/**
674674
Set the end edge (LTR=right, RTL=left) distance from the container end edge in
675675
percentage of its container width.
676-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
676+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
677677
*/
678678
@discardableResult
679679
public func end(_ percent: FPercent) -> Flex {
@@ -682,20 +682,22 @@ public final class Flex {
682682
}
683683

684684
/**
685-
Set the left and right edges distance from the container edges in pixels.
686-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
687-
*/
685+
Set the left and right edges distance from the container edges in pixels.
686+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
687+
For relative position, the right edge will be ignored.
688+
*/
688689
@discardableResult
689690
public func horizontally(_ value: CGFloat) -> Flex {
690691
yoga.left = YGValue(value)
691692
yoga.right = YGValue(value)
692693
return self
693694
}
694695

695-
/**
696-
Set the left and right edges distance from the container edges in percentage of its container width.
697-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
698-
*/
696+
/**
697+
Set the left and right edges distance from the container edges in percentage of its container width.
698+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
699+
For relative position, the right edge will be ignored.
700+
*/
699701
@discardableResult
700702
public func horizontally(_ percent: FPercent) -> Flex {
701703
yoga.left = YGValue(value: Float(percent.value), unit: .percent)
@@ -705,7 +707,8 @@ public final class Flex {
705707

706708
/**
707709
Set the top and bottom edges distance from the container edges in pixels.
708-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
710+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
711+
For relative position, the bottom edge will be ignored.
709712
*/
710713
@discardableResult
711714
public func vertically(_ value: CGFloat) -> Flex {
@@ -716,7 +719,8 @@ public final class Flex {
716719

717720
/**
718721
Set the top and bottom edges distance from the container edges in percentage of its container height.
719-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
722+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
723+
For relative position, the bottom edge will be ignored.
720724
*/
721725
@discardableResult
722726
public func vertically(_ percent: FPercent) -> Flex {
@@ -727,7 +731,8 @@ public final class Flex {
727731

728732
/**
729733
Set all edges distance from the container edges in pixels.
730-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
734+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
735+
For relative position, the bottom and right edges will be ignored.
731736
*/
732737
@discardableResult
733738
public func all(_ value: CGFloat) -> Flex {
@@ -740,7 +745,8 @@ public final class Flex {
740745

741746
/**
742747
Set all edges distance from the container edges in percentage of its container size.
743-
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
748+
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
749+
For relative position, the bottom and right edges will be ignored.
744750
*/
745751
@discardableResult
746752
public func all(_ percent: FPercent) -> Flex {
@@ -1424,10 +1430,12 @@ public final class Flex {
14241430
/**
14251431
*/
14261432
public enum Position {
1427-
/// Default value.
1433+
/// Default value. Positioned according to the flex container's flow. The item is positioned using Insets properties (top, bottom, left, right, start, end) from its normal position within its flex container and will take up space within the flex container. This node will always form a containing block.
14281434
case relative
1429-
/// Positioned absolutely in regards to its container. The item is positionned using properties top, bottom, left, right, start, end.
1435+
/// Positioned absolutely, removed from the flex container's flow. The item is positioned using Insets properties (top, bottom, left, right, start, end). Insets will offset the node from its containing block.
14301436
case absolute
1437+
/// Positioned like relative but ignores insets and will not form a containing block.
1438+
case `static`
14311439
}
14321440

14331441
/**

Sources/Swift/Impl/FlexLayout+Enum.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extension YGWrap {
5353
extension YGPositionType {
5454
static let relative = YGPositionTypeRelative
5555
static let absolute = YGPositionTypeAbsolute
56+
static let `static` = YGPositionTypeStatic
5657
}
5758

5859
extension YGDirection {
@@ -149,6 +150,7 @@ extension Flex.Position {
149150
switch self {
150151
case .relative: return YGPositionType.relative
151152
case .absolute: return YGPositionType.absolute
153+
case .static: return YGPositionType.static
152154
}
153155
}
154156
}

0 commit comments

Comments
 (0)