Skip to content

Commit

Permalink
Added documentation to equalWidthPreferred and equalHeightPreferred
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanlintott committed Jun 4, 2024
1 parent 5bc3f2e commit 42254f1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
49 changes: 49 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,55 @@ struct OnSizeChangeExample: View {
}
```

### equalWidthPreferred
Views with `.equalWidthPreferred()` inside a view with `.equalWidthContainer()` will have widths equal to the largest view. If space is limited views will shrink equally, each to a minimum size that fits the content. It works in `HStack`, `VStack` and even custom layouts like `HFlow`

This works especially well in macOS when you want all buttons on a window to have the same width while still allowing some shrinking.

```swift
HStack {
Button { } label: {
Text("More Information")
.equalWidthPreferred()
}
Spacer()

Button { } label: {
Text("Cancel")
.equalWidthPreferred()
}

Spacer()

Button { } label: {
Text("OK")
.equalWidthPreferred()
}
}
.equalWidthContainer()
.frame(maxWidth: 400)
.padding()
```

### equalHeightPreferred
Views with `.equalHeightPreferred()` inside a view with `.equalHeightContainer()` will have heights equal to the largest view. If space is limited views will shrink equally, each to a minimum size that fits the content. It works in `HStack`, `VStack` and even custom layouts like `VFlow`

```swift
HStack {
Group {
Text("Here's something with some text")

Text("And more")
}
.foregroundColor(.white)
.padding()
.equalHeightPreferred()
.background(Color.blue.cornerRadius(10))
}
.equalHeightContainer()
.frame(maxWidth: 200)
```

### keyboardHeight
An environment variable that will update with animation as the iOS keyboard appears and disappears. It will always be zero for non-iOS platforms.

Expand Down
2 changes: 2 additions & 0 deletions Sources/FrameUp/FrameAdjustment/EqualSize/EqualHeight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ struct EqualHeightContainerViewModifier: ViewModifier {
}

public extension View {
/// This view will have a height equal to the largest view with this modifier inside a view with `.equalHeightContainer()`. If space is limited views will shrink equally, each to a minimum fixed size that fits the content.
func equalHeightPreferred() -> some View {
modifier(EqualHeightViewModifier())
}

/// Views inside this view using `.equalHeightPreferred()` will have a height equal to the largest view with that modifier. If space is limited these views will shrink equally, each to a minimum fixed size that fits the content.
func equalHeightContainer() -> some View {
modifier(EqualHeightContainerViewModifier())
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/FrameUp/FrameAdjustment/EqualSize/EqualWidth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ private struct EqualWidthContainerViewModifier: ViewModifier {
}

public extension View {
/// Adds an action to perform when parent view size value changes.
/// - Parameter action: The action to perform when the size changes. The action closure passes the new value as its parameter.
/// - Returns: A view with an invisible background `GeometryReader` that detects and triggers an action when the size changes.
/// This view will have a width equal to the largest view with this modifier inside a view with `.equalWidthContainer()`. If space is limited views will shrink equally, each to a minimum fixed size that fits the content.
func equalWidthPreferred() -> some View {
modifier(EqualWidthViewModifier())
}

/// Views inside this view using `.equalWidthPreferred()` will have a width equal to the largest view with that modifier. If space is limited these views will shrink equally, each to a minimum fixed size that fits the content.
func equalWidthContainer() -> some View {
modifier(EqualWidthContainerViewModifier())
}
Expand All @@ -78,6 +77,7 @@ public extension View {
Text("More Information")
.equalWidthPreferred()
}

Spacer()

Button { } label: {
Expand Down

0 comments on commit 42254f1

Please sign in to comment.