Skip to content

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.

License

Notifications You must be signed in to change notification settings

GlennChiu/FittingHStack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages