How do I alignment guides in SwiftUI?
Asked on 2024-07-30
1 search
To align guides in SwiftUI, you can use the alignmentGuide
modifier. This modifier allows you to specify how a view should align itself relative to its parent or other sibling views. Here’s a basic example:
struct ContentView: View {
var body: some View {
HStack(alignment: .top) {
Text("Hello")
.alignmentGuide(.top) { d in d[.bottom] }
Text("World")
}
}
}
In this example, the alignmentGuide
modifier is used to align the bottom of the "Hello" text with the top of the "World" text.
For more advanced layouts and dynamic adjustments, you might want to explore the new features and improvements in SwiftUI presented at WWDC. For example, the session What’s new in SwiftUI covers various enhancements, including scrolling and alignment controls, which could be useful depending on your specific needs.
Relevant Sessions
These sessions provide a comprehensive overview of the latest SwiftUI capabilities, including layout and alignment techniques.

Build multilingual-ready apps
Ensure your app works properly and effectively for multilingual users. Learn best practices for text input, display, search, and formatting. Get details on typing in multiple languages without switching between keyboards. And find out how the latest advances in the String Catalog can make localization even easier.

Migrate your TVML app to SwiftUI
SwiftUI helps you build great apps on all Apple platforms and is the preferred toolkit for bringing your content into the living room with tvOS 18. Learn how to use SwiftUI to create familiar layouts and controls from TVMLKit, and get tips and best practices.

Get started with HealthKit in visionOS
Discover how to use HealthKit to create experiences that take full advantage of the spatial canvas. Learn the capabilities of HealthKit on the platform, find out how to bring an existing iPadOS app to visionOS, and explore the special considerations governing HealthKit during a Guest User session. You’ll also learn ways to use SwiftUI, Swift Charts, and Swift concurrency to craft innovative experiences with HealthKit.