I am building a simple form in SwiftUI for macOS but once I add a picker the layout is screwed up.
This is my code:
var body: some View {
GeometryReader { geometry in
Form {
HStack {
Text("Label")
.frame(minWidth: 0.25 * geometry.size.width, alignment: .leading)
TextField("", text: $field1)
}
HStack {
Text("Long Label")
.frame(minWidth: 0.25 * geometry.size.width, alignment: .leading)
TextField("", text: $field2)
}
// HStack {
// Text("Picker")
// .frame(minWidth: 0.25 * geometry.size.width, alignment: .leading)
//
// Picker("", selection: $selectedColor) {
// ForEach(colors, id: \.self) {
// Text($0)
// }
// }
// }
}
.padding(20)
}
}
}
and this is the form which comes out:
Adding a simple picker, the layout becomes:
and I am not able to keep all the labels aligned. I have tried to add a frame to the picker and to the HStack but nothing helps.
I have also tried with:
Picker(selection: $selectedColor, label: EmptyView() {
...
}
and:
Picker(selection: $selectedColor, label: Text("Picker") {
...
}
getting the same result.
How do I keep the labels aligned?