I'm creating a form with multiple sections in SwiftUI. Each section contains several text fields, each with a label.
I'd like to align the leading edges of the text fields with each other and also with the center of the view.
I'm able to align the leading edges of the text fields within a section using the following:
extension HorizontalAlignment {
private struct LeadingEdgeAlignment: AlignmentID {
/// A custom alignment for leading edges.
static func defaultValue(in context: ViewDimensions) -> CGFloat {
context[.leading]
}
}
/// A guide for aligning leading edges.
static let leadingEdge = HorizontalAlignment(LeadingEdgeAlignment.self)
}
struct ContentView: View {
var body: some View {
Form {
Section(header: Text("Section One")) {
VStack(alignment: .leadingEdge) {
HStack {
Text("Primary")
TextField("", text: .constant(""))
.frame(minWidth: nil, idealWidth: nil, maxWidth: 75, minHeight: nil, maxHeight: nil)
.alignmentGuide(.leadingEdge) { $0[HorizontalAlignment.leading] }
.textFieldStyle(RoundedBorderTextFieldStyle())
}
HStack {
Text("Secondary")
TextField("", text: .constant(""))
.frame(minWidth: nil, idealWidth: nil, maxWidth: 75, minHeight: nil, maxHeight: nil)
.alignmentGuide(.leadingEdge) { $0[HorizontalAlignment.leading] }
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
}
Section(header: Text("Section Two")) {
VStack(alignment: .leadingEdge) {
HStack {
Text("Milk")
TextField("", text: .constant(""))
.frame(minWidth: nil, idealWidth: nil, maxWidth: 75, minHeight: nil, maxHeight: nil)
.alignmentGuide(.leadingEdge) { $0[HorizontalAlignment.leading] }
.textFieldStyle(RoundedBorderTextFieldStyle())
}
HStack {
Text("Cheesecake")
TextField("", text: .constant(""))
.frame(minWidth: nil, idealWidth: nil, maxWidth: 75, minHeight: nil, maxHeight: nil)
.alignmentGuide(.leadingEdge) { $0[HorizontalAlignment.leading] }
.textFieldStyle(RoundedBorderTextFieldStyle())
}
}
}
}
}
}
However, so far all my attempts to create alignment guides that span sections have failed.
How does one align views across form sections?