Contents

glennchiu/fittinghstack

A lightweight SwiftUI layout that behaves like an `HStack`, but **automatically wraps its child views** onto new lines when there’s not enough horizontal space.

✨ Features

  • 🧩 Drop-in replacement for HStack
  • 🔄 Automatically wraps to a new line when space runs out
  • ⚙️ Customizable spacing and lineSpacing
  • 📱 Works with dynamic type, animations, and layout changes
  • 💡 Built using SwiftUI’s modern Layout protocol (iOS 16+)

🚀 Example

import SwiftUI
import FittingHStack

struct ExampleView: View {
    let tags = ["Swift", "SwiftUI", "Combine", "Async/Await", "Concurrency", "iOS 26", "Layout Protocol"]

    var body: some View {
        FittingHStack(spacing: 10, lineSpacing: 10) {
            ForEach(tags, id: \.self) { tag in
                Text(tag)
                    .padding(.horizontal, 10)
                    .padding(.vertical, 6)
                    .background(Capsule().fill(Color.blue.opacity(0.2)))
            }
        }
        .padding()
    }
}

Result:

Tags are displayed horizontally and wrap neatly into multiple rows when the available width is exceeded.


📦 Installation

You can copy the FittingHStack.swift file directly into your project, or add it as a Swift Package dependency:

.package(url: "https://github.com/GlennChiu/FittingHStack.git", from: "1.1.1")

Then import it:

import FittingHStack

🧩 Requirements

  • iOS 16.0+ / macOS 13.0+
  • Swift 5.7+
  • Xcode 14+

🫶 Credits

Created by Glenn Chiu — inspired by the simplicity of SwiftUI stacks and the flexibility of flow layouts.


🪪 License

FittingHStack is available under the MIT License. See the LICENSE file for more info.

Package Metadata

Repository: glennchiu/fittinghstack

Default branch: master

README: README.md