Unable to align ForEach to conform to leading aligment
So I have the following code:
VStack(alignment: .leading, spacing: 0) {
HStack(alignment: .center, spacing: 3) {
Text("Hours")
.font(.body.weight(.bold))
}
ScrollViewIfNeeded(.vertical, showsIndicators: false) {
Group {
if item.hours != [] || !item.hours.isEmpty {
ForEach(item.hours, id: \.self) { hour in
Text(hour)
}
} else {
Text("No hours provided.")
}
}
.font(.footnote)
.foregroundColor(.secondary)
}
}
.frame(width: containerWidth * 0.40)
Which won't conform to the .leading
modif开发者_如何学Ciers at all - The items are currently centered in the view.
If I remove the ForEach statement and just place multiple regular Text("..")
calls, it works just fine and aligns to the left, similar to this:
VStack(alignment: .leading, spacing: 0) {
HStack(alignment: .center, spacing: 3) {
Text("Hours")
.font(.body.weight(.bold))
}
ScrollViewIfNeeded(.vertical, showsIndicators: false) {
Group {
Text("Hello1")
Text("Hello2")
Text("Hello3")
}
.font(.footnote)
.foregroundColor(.secondary)
}
}
.frame(width: containerWidth * 0.40)
The above works just fine and aligns to the left.
Does anyone know what difference ForEach statements have with alignment?
Update:
let item: Place
<- Definition of item.
struct Place: Codable, Identifiable {
var hours = [String]()
// MARK: - SET DEFAULT PROPERTIES
static var `default` : Place {
Place(
hours: ("Hours")
)
}
init(
hours: (String)
) {
self.hours = [hours]
}
init(with p: MGLocation) {
self.hours = p.hours ?? []
}
}
I figured it out, weird enough, you have to pass a VStack inside a ScrollView, regardless if you have a VStack defined outside the ScrollView:
ScrollViewIfNeeded(.vertical, showsIndicators: false) {
Group {
VStack(alignment: .leading) {
if item.hours != [] || !item.hours.isEmpty {
ForEach(item.hours, id: \.self) { hour in
Text(hour)
}
.frame(alignment: .topLeading)
} else {
Text("No hours provided.")
}
}
}
.font(.footnote)
.foregroundColor(.secondary)
}
Odd, but it works!
精彩评论