Why does Spacer() not work? (between Text and Stepper)

Why does my Spacer() between Text and Stepper not work in the code below?

Here is how it looks like:

enter image description here

Here is how I would like it to look like:

enter image description here

import SwiftUI

struct FontEditView: View {
    @Binding var fontSize: CGFloat
    
    var body: some View {
        NavigationStack {
            Form {
                HStack {
                    Text("Font Size")
                    Spacer()
                    Stepper(fontSize.formatted(), value: $fontSize, in: 5...50)
                }    
            }
        }
    }
}

struct FontEditView_Previews: PreviewProvider {
    static var previews: some View {
        FontEditView(fontSize: .constant(20))
    }
}

>Solution :

Just add .fixedSize() to the Stepper

Fixes this view at its ideal size.

Stepper(fontSize.formatted(), value: $fontSize, in: 5...50)
    .fixedSize()

Leave a Reply