BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

Label not near Picker when using ".grouped" for a "Form"

Forums > macOS

I have a problem with the display of a picker within “Form”. I enter the following code

import SwiftUI
struct ContentView: View {
    @State var myPickerSelection = 1.0
    var body: some View {
        Form {
            HStack {
                Text("My parameter")
                Spacer(minLength: 100)
                Slider(value: $myPickerSelection, in: 0.0...1.0,
                             label: { Text("\(myPickerSelection, specifier: "%.2f")") })
                .padding()
            }
        }
        .padding()
    }
}

so the appearance is correct in my opinion. The label is near the Picker

But if I enter the code with the “.grouped” option

import SwiftUI
struct ContentView: View {
    @State var myPickerSelection = 1.0
    var body: some View {
        Form {
            HStack {
                Text("My parameter")
                Spacer(minLength: 100)
                Slider(value: $myPickerSelection, in: 0.0...1.0,
                             label: { Text("\(myPickerSelection, specifier: "%.2f")") })
                .padding()
            }
        }
        .formStyle(.grouped)
        .padding()
    }
}

The “picker-label” has a distance to the picker-display

How can this be fixed. I have no idea, unless I implement an “HSTack” like the individual components get a fixed size (”.frame(with: ...)”

Are there any other ideas?

   

i have not tried your code, so bear that in mind... have you tried using LabeledContent? This lets you define a label and content separately, similar to using the HStack, but optimised for forms.

   

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.