To utilize an alignment guide it appears that the parent container must indicate the alignment type listed in the guide. Other wise the alignment guide is not called. So I removed all of the alignments and alignment guides. Then stepped down thru the views starting with the outermost. At the outmost view and each subview I set the alignment to leading. As a result, if I set the alignment guide to be HorizontalAlignment.leading, the associated closure is run and the guide is applied. I should note that I had to insert a V Stack directly inside the Natigation Stack to get things to work, and of course set its alignment to leading. I have not yet determined how to replace the spacers with vertical alignment guides but will work on that next. Below is a view of the resutling output and the associated code. The code for sections "Group 2" and "Group 3" are essentially identical to that for "Group 1", so I did not include them. In addition, the Navigation Destination did not change so it was also not included.
struct ContentView: View {
var body: some View {
NavigationStack {
VStack (alignment: HorizontalAlignment.leading) {
Text("Group1")
.font(Font.custom("Arial", size: 16))
.alignmentGuide(HorizontalAlignment.leading, computeValue: { viewDimensions in
return viewDimensions[HorizontalAlignment.leading] - 42
})
ZStack (alignment: Alignment(horizontal: .leading, vertical: .center)) {
RoundedRectangle(cornerRadius: 10)
.fill(.white)
.alignmentGuide(HorizontalAlignment.leading, computeValue: { viewDimensions in
return viewDimensions[HorizontalAlignment.leading] - 30
})
VStack (alignment: HorizontalAlignment.leading) {
NavigationLink(value: "vooTable") {
Text("Data Table")
.alignmentGuide(HorizontalAlignment.leading, computeValue: { viewDimensions in
return viewDimensions[HorizontalAlignment.leading] - 55
})
}
.font(Font.custom("Arial", size: 14))
.buttonStyle(.link)
.underline()
.focusable(false)
} // end vstack
} // end zStack
.frame(width: 200, height: 60, alignment: .leading)
Spacer()
.frame(width: 200, height: 25, alignment: .leading)
} // end navigation destination
} // end v stack
} // end navigtion stack
.frame(width: 750, height: 550, alignment: Alignment(horizontal: .leading, vertical: .center))
}
Topic:
Programming Languages
SubTopic:
Swift
Tags: