Skip to content

Latest commit

 

History

History
81 lines (55 loc) · 1.95 KB

File metadata and controls

81 lines (55 loc) · 1.95 KB

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. Perfect for tags, chips, buttons, or any horizontally flowing content that needs to adapt gracefully to screen size.


✨ 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.