Why this happens
SwiftUI’s HStack distributes space evenly among its children by default. When you place a button on the left and a single image on the right, the image sits at the trailing edge of the remaining space—not at the horizontal center of the whole stack. Adding a Spacer() between them pushes the image to the far right, while omitting the spacer leaves the image hugging the button. Neither produces a centered image.
The root cause is that HStack has no concept of “center of the parent” when the leading and trailing items have different widths. The layout engine only knows the space each child requests and the spacers you insert.
How to fix it
1. ZStack with two independent HStacks (recommended)
Overlay two HStack instances inside a ZStack. The first HStack holds the left-aligned button and a trailing spacer. The second HStack contains only the centered view (optionally wrapped in spacers). Because each HStack spans the full width, the centered view aligns to the true horizontal center.
struct CenteredNavBar: View {
var body: some View {
ZStack {
// Left-aligned button
HStack {
Button(action: { print("Back") }) {
Image(systemName: "chevron.left")
.font(.title2)
.frame(width: 44, height: 44)
}
.padding(.leading, 16)
Spacer()
}
// Centered title / image
HStack {
Spacer()
Image(systemName: "logo.twitter")
.font(.title2)
Spacer()
}
}
.frame(height: 44)
}
}
Why this works: Each HStack independently occupies the full width of the ZStack. The first stack’s spacer pushes the button to the leading edge; the second stack’s spacers center its single child. No width calculations or magic numbers are required.
2. Negative leading padding matched to the left item’s width
If you prefer a single HStack, measure the left button’s width and apply an equal negative leading padding to the centered view. This pulls the centered view back toward the middle by the exact amount the button offsets it.
struct NegativePaddingBar: View {
private let buttonWidth: CGFloat = 44
var body: some View {
HStack {
Button(action: { print("Back") }) {
Image(systemName: "chevron.left")
.font(.title2)
.frame(width: buttonWidth, height: buttonWidth)
}
.padding(.leading, 16)
Spacer()
Image(systemName: "logo.twitter")
.font(.title2)
.padding(.leading, -buttonWidth) // compensate for button width
Spacer() // keeps the view centered if trailing space differs
}
.frame(height: 44)
}
}
Caveats: You must know the button’s exact width at compile time (or pass it via @State/PreferenceKey for dynamic sizes). If the button’s width changes—due to localization, Dynamic Type, or conditional content—the centering drifts.
3. GeometryReader for fully dynamic widths (advanced)
When the left item’s width isn’t fixed, read it at runtime with a GeometryReader and a PreferenceKey, then apply the measured width as negative padding.
struct WidthKey: PreferenceKey {
static var defaultValue: CGFloat = 0
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) {
value = nextValue()
}
}
struct DynamicCenteredBar: View {
@State private var leftWidth: CGFloat = 0
var body: some View {
HStack {
Button(action: { print("Back") }) {
Image(systemName: "chevron.left")
.font(.title2)
.frame(width: 44, height: 44)
}
.padding(.leading, 16)
.background(GeometryReader { geo in
Color.clear.preference(key: WidthKey.self, value: geo.size.width)
})
Spacer()
Image(systemName: "logo.twitter")
.font(.title2)
.padding(.leading, -leftWidth)
Spacer()
}
.frame(height: 44)
.onPreferenceChange(WidthKey.self) { leftWidth = $0 }
}
}
Use this only when the left item’s width truly varies at runtime; otherwise the ZStack approach is simpler and more performant.
How to prevent it
- Default to the ZStack + two HStacks pattern for navigation-bar-style layouts.
- Avoid hard-coded frame widths on the centered view; let it size to its content.
- If you use negative padding, expose the left item’s width as a constant or
@Bindingso both sides stay in sync. - Test with Dynamic Type (large accessibility sizes) and RTL languages to verify the center stays centered.
FAQ
Does the ZStack approach work inside a NavigationStack toolbar?
Yes. Place the ZStack in a .toolbar { ToolbarItem(placement: .principal) { … } } or as a custom .navigationBarTitleDisplayMode(.inline) replacement. The principal item centers automatically, but a custom ZStack gives you full control over left/center/right alignment.
Why does my centered image still look slightly off-center?
Check for hidden padding on the image itself (.padding(), .frame(alignment:)) or on the containing HStack. Remove any explicit alignment on the centered view’s frame and ensure both HStack spacers have equal priority (default).
Can I add a right-side button without breaking the centering?
Absolutely. In the ZStack solution, add a third HStack with a leading spacer and the right button. In the negative-padding solution, add the right button after the second spacer and increase the negative padding by the right button’s width (or switch to the ZStack approach, which handles three-way alignment naturally).
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.
