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.
- 🧩 Drop-in replacement for
HStack - 🔄 Automatically wraps to a new line when space runs out
- ⚙️ Customizable
spacingandlineSpacing - 📱 Works with dynamic type, animations, and layout changes
- 💡 Built using SwiftUI’s modern
Layoutprotocol (iOS 16+)
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.
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- iOS 16.0+ / macOS 13.0+
- Swift 5.7+
- Xcode 14+
Created by Glenn Chiu — inspired by the simplicity of SwiftUI stacks and the flexibility of flow layouts.
FittingHStack is available under the MIT License.
See the LICENSE file for more info.