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 static position feature #254

Merged
merged 11 commits into from
Jun 16, 2024
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
8 changes: 4 additions & 4 deletions FlexLayout.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
24AE97411FEAF30A00995987 /* UIView+FlexLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24AE97321FEAF30A00995987 /* UIView+FlexLayout.swift */; };
24AE97421FEAF30A00995987 /* Percent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24AE97331FEAF30A00995987 /* Percent.swift */; };
24AE974A1FEAF30A00995987 /* YGLayoutExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 24AE973D1FEAF30A00995987 /* YGLayoutExtensions.swift */; };
711F2EE62ACC727000DDDD67 /* AbsolutionPositionContentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711F2EE02ACC727000DDDD67 /* AbsolutionPositionContentTests.swift */; };
711F2EE72ACC727000DDDD67 /* WidthSizeContentTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711F2EE12ACC727000DDDD67 /* WidthSizeContentTests.swift */; };
711F2EE82ACC727000DDDD67 /* PaddingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711F2EE22ACC727000DDDD67 /* PaddingTests.swift */; };
711F2EE92ACC727000DDDD67 /* FlexLayoutTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 711F2EE32ACC727000DDDD67 /* FlexLayoutTests.swift */; };
Expand Down Expand Up @@ -99,6 +98,7 @@
EF0E61762C1A811100DF30F6 /* YGNodeStyle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EF0E612C2C1A811100DF30F6 /* YGNodeStyle.cpp */; };
EF0E61772C1A811100DF30F6 /* YGPixelGrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EF0E612D2C1A811100DF30F6 /* YGPixelGrid.cpp */; };
EF0E61782C1A811100DF30F6 /* YGValue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EF0E612E2C1A811100DF30F6 /* YGValue.cpp */; };
EF5445402C1D607100BF4A33 /* InsetTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF54453F2C1D607100BF4A33 /* InsetTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -138,7 +138,6 @@
24DA37631EF843C500D1AB2F /* FlexLayoutTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FlexLayoutTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
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>"; };
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>"; };
711F2EE02ACC727000DDDD67 /* AbsolutionPositionContentTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AbsolutionPositionContentTests.swift; sourceTree = "<group>"; };
711F2EE12ACC727000DDDD67 /* WidthSizeContentTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WidthSizeContentTests.swift; sourceTree = "<group>"; };
711F2EE22ACC727000DDDD67 /* PaddingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PaddingTests.swift; sourceTree = "<group>"; };
711F2EE32ACC727000DDDD67 /* FlexLayoutTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FlexLayoutTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -223,6 +222,7 @@
EF0E612C2C1A811100DF30F6 /* YGNodeStyle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGNodeStyle.cpp; sourceTree = "<group>"; };
EF0E612D2C1A811100DF30F6 /* YGPixelGrid.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGPixelGrid.cpp; sourceTree = "<group>"; };
EF0E612E2C1A811100DF30F6 /* YGValue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = YGValue.cpp; sourceTree = "<group>"; };
EF54453F2C1D607100BF4A33 /* InsetTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InsetTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -304,12 +304,12 @@
711F2EDF2ACC727000DDDD67 /* FlexLayoutTests */ = {
isa = PBXGroup;
children = (
711F2EE02ACC727000DDDD67 /* AbsolutionPositionContentTests.swift */,
711F2EE12ACC727000DDDD67 /* WidthSizeContentTests.swift */,
711F2EE22ACC727000DDDD67 /* PaddingTests.swift */,
711F2EE32ACC727000DDDD67 /* FlexLayoutTests.swift */,
711F2EE42ACC727000DDDD67 /* MarginTests.swift */,
711F2EE52ACC727000DDDD67 /* JustifyContentTests.swift */,
EF54453F2C1D607100BF4A33 /* InsetTests.swift */,
);
path = FlexLayoutTests;
sourceTree = "<group>";
Expand Down Expand Up @@ -715,8 +715,8 @@
711F2EEA2ACC727000DDDD67 /* MarginTests.swift in Sources */,
711F2EE92ACC727000DDDD67 /* FlexLayoutTests.swift in Sources */,
711F2EE82ACC727000DDDD67 /* PaddingTests.swift in Sources */,
711F2EE62ACC727000DDDD67 /* AbsolutionPositionContentTests.swift in Sources */,
711F2EEB2ACC727000DDDD67 /* JustifyContentTests.swift in Sources */,
EF5445402C1D607100BF4A33 /* InsetTests.swift in Sources */,
711F2EE72ACC727000DDDD67 /* WidthSizeContentTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- FlexLayout (2.0.7)
- FlexLayout (2.0.8)
- PinLayout (1.10.5)
- SwiftLint (0.55.1)

Expand All @@ -18,7 +18,7 @@ EXTERNAL SOURCES:
:path: "./"

SPEC CHECKSUMS:
FlexLayout: fcb9aaa754029afff0cb99e9cdfe10c68d70b6ac
FlexLayout: 74c80db7698ea6e0d11e00ba012c0b9f3252938a
PinLayout: f6c2b63a5a5b24864064e1d15c67de41b4e74748
SwiftLint: 3fe909719babe5537c552ee8181c0031392be933

Expand Down
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Flexbox is an incredible improvement over UIStackView. It is simpler to use, muc
* [Creation, modification and definition of flexbox containers](#create_modify_define_containers)
* [Flexbox containers properties](#containers_properties)
* [Flexbox items properties](#intems_properties)
* [Absolute positioning](#absolute_positioning)
* [Positioning](#positioning)
* [Adjusting the size](#adjusting_size)
* [Width, height and size](#width_height_size)
* [minWidth, maxWidth, minHeight, maxHeight](#minmax_width_height_size)
Expand Down Expand Up @@ -778,25 +778,32 @@ Item natural size, considering only properties of the view itself. Independent o

<br>

<a name="absolute_positioning"></a>
## 4. Absolute positioning
<a name="positioning"></a>
## 4. Positioning
- Applies to: `flex items`
- Parameter: CGFloat

**Method:**

* **`position(_: Position)`**
The position property tells Flexbox how you want your item to be positioned within its parent. Position values:
* **`relative (default)`**
* **`absolute`**: The view is positioned using properties: top(), bottom(), left(), right(), start(), end().
* **`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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are any awkward phrases or sentences that do not align with the project's tone, please let me know.

* **`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.
* **`static`**: The view ignores insets and will not form a containing block.

###### Usage examples:
```swift
view.flex.position(.relative).top(10).left(10).size(50)

view.flex.position(.absolute).top(10).left(10).size(50)
```

### 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:
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.

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.

This is done through the following methods:

**Methods:**

Expand All @@ -819,10 +826,10 @@ Controls the distance child’s left and right edges from the parent’s edges.
* **`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.

###### Usage examples:
```swift
view.flex.position(.relative).top(10).right(10).width(100).height(50)

view.flex.position(.absolute).top(10).right(10).width(100).height(50)
view.flex.position(.absolute).left(20%).right(20%)
```
Expand Down
60 changes: 34 additions & 26 deletions Sources/Swift/FlexLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ public final class Flex {
}

//
// MARK: Absolute positionning
// MARK: Position / Inset
//

/**
Expand All @@ -561,7 +561,7 @@ public final class Flex {

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

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

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

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

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

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

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

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

/**
Set the start edge (LTR=left, RTL=right) distance from the container start edge in pixels.
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
*/
@discardableResult
public func start(_ value: CGFloat) -> Flex {
Expand All @@ -652,7 +652,7 @@ public final class Flex {
/**
Set the start edge (LTR=left, RTL=right) distance from the container start edge in
percentage of its container width.
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
*/
@discardableResult
public func start(_ percent: FPercent) -> Flex {
Expand All @@ -662,7 +662,7 @@ public final class Flex {

/**
Set the end edge (LTR=right, RTL=left) distance from the container end edge in pixels.
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
*/
@discardableResult
public func end(_ value: CGFloat) -> Flex {
Expand All @@ -673,7 +673,7 @@ public final class Flex {
/**
Set the end edge (LTR=right, RTL=left) distance from the container end edge in
percentage of its container width.
This method is valid only when the item position is absolute (`view.flex.position(.absolute)`)
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
*/
@discardableResult
public func end(_ percent: FPercent) -> Flex {
Expand All @@ -682,20 +682,22 @@ public final class Flex {
}

/**
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)`)
*/
Set the left and right edges distance from the container edges in pixels.
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
For relative position, the right edge will be ignored.
*/
@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)`)
*/
/**
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 or relative. It is not valid when the position is static.
For relative position, the right edge will be ignored.
*/
@discardableResult
public func horizontally(_ percent: FPercent) -> Flex {
yoga.left = YGValue(value: Float(percent.value), unit: .percent)
Expand All @@ -705,7 +707,8 @@ public final class Flex {

/**
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)`)
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
For relative position, the bottom edge will be ignored.
*/
@discardableResult
public func vertically(_ value: CGFloat) -> Flex {
Expand All @@ -716,7 +719,8 @@ public final class Flex {

/**
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)`)
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
For relative position, the bottom edge will be ignored.
*/
@discardableResult
public func vertically(_ percent: FPercent) -> Flex {
Expand All @@ -727,7 +731,8 @@ public final class Flex {

/**
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)`)
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
For relative position, the bottom and right edges will be ignored.
*/
@discardableResult
public func all(_ value: CGFloat) -> Flex {
Expand All @@ -740,7 +745,8 @@ public final class Flex {

/**
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)`)
This method is valid only when the item position is absolute or relative. It is not valid when the position is static.
For relative position, the bottom and right edges will be ignored.
*/
@discardableResult
public func all(_ percent: FPercent) -> Flex {
Expand Down Expand Up @@ -1424,10 +1430,12 @@ public final class Flex {
/**
*/
public enum Position {
/// Default value.
/// 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.
case relative
/// Positioned absolutely in regards to its container. The item is positionned using properties top, bottom, left, right, start, end.
/// 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.
case absolute
/// Positioned like relative but ignores insets and will not form a containing block.
case `static`
}

/**
Expand Down
2 changes: 2 additions & 0 deletions Sources/Swift/Impl/FlexLayout+Enum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extension YGWrap {
extension YGPositionType {
static let relative = YGPositionTypeRelative
static let absolute = YGPositionTypeAbsolute
static let `static` = YGPositionTypeStatic
}

extension YGDirection {
Expand Down Expand Up @@ -149,6 +150,7 @@ extension Flex.Position {
switch self {
case .relative: return YGPositionType.relative
case .absolute: return YGPositionType.absolute
case .static: return YGPositionType.static
}
}
}
Expand Down
Loading
Loading