Swiftpack.co - reuschj/RotatableStack as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by reuschj.
reuschj/RotatableStack 1.1.1
A SwiftUI view that embeds in a VStack when in portrait orientation and HStack in landscape orientation. It wraps your content in a GeometryReader to determine if the device is in landscape or portrait rotation (or on larger devices, if it's generally wider or narrower). If portrait, it wraps the content in a VStack, else it wraps in an HStack for landscape.
⭐️ 8
🕓 27 weeks ago
.package(url: "https://github.com/reuschj/RotatableStack.git", from: "1.1.1")

RotatableStack

A SwiftUI view that embeds in a VStack when in portrait orientation and HStack in landscape orientation. It wraps your content in a GeometryReader to determine if the device is in landscape or portrait rotation (or on larger devices, if it's generally wider or narrower). If portrait, it wraps the content in a VStack, else it wraps in an HStack for landscape.

Basic Usage

Use RotatableStack in place of a VStack or HStack where you want the layout to change based on the geometry of your device:

struct MyView: View {

    var body: some View {
        RotatableStack {
            Spacer()
            Text("Hello, World!")
        }
    }
}

Alignment and Spacing

You can also pass parameters down to the underlying VStack or HStack, including alignment and spacing.

struct MyView: View {

    var body: some View {
        RotatableStack(
            horizontalAlignment: .leading,
            verticalAlignment: .top,
            spacing: 16
        ) {
            Spacer()
            Text("Hello, World!")
        }
    }
}
Parameter Type Default Notes
horizontalAlignment HorizontalAlignment .center Horizontal alignment passed to underlying VStack when alignment is portrait.
verticalAlignment VerticalAlignment .center Vertical alignment passed to underlying HStack when alignment is landscape.
spacing CGFloat? nil Spacing to pass to the underlying VStack or HStack.

You pass both alignment types since one will be used for portrait and the other for landscape.

If you want to make this a bit easier, this package provides a FlexAlignment enum that works almost exactly the same as HorizontalAlignment, but will work in both orientations. In landscape, the top is "leading" and the bottom is "trailing". This is just a shortcut if you want to keep alignment consistent in either orientation.

struct MyView: View {

    var body: some View {
        RotatableStack(
            alignment: .leading,
            spacing: 16
        ) {
            Spacer()
            Text("Hello, World!")
        }
    }
}
Parameter Type Default Notes
alignment FlexAlignment NA A universal alignment that is translated/passed to underlying VStack when alignment is portrait or HStack when alignment is landscape.
spacing CGFloat? nil Spacing to pass to the underlying VStack or HStack.

Thanks to kcochibili for suggesting the addition of alignment and spacing!

GitHub

link
Stars: 8
Last commit: 27 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

Adds Alignment and Spacing Parameters
27 weeks ago
  • Adds new optional passthrough parameters for alignment and spacing.
  • These are passed to the underlying VStack or HStack
  • README documentation updated in this fix release

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics