For the sake of practicing, am trying to make a Worlde type app, nothing original.
I would like to make it so that the ScrollView, the list of words at the top of the VStack, is aligned to the left side of the screen. Even tried embedding it its own VStack with an alignment of .leading, but obviously to no avail.
Is there any alignment option I can use, or must I go to GeometeryReader for something like this?
import SwiftUI
struct ContentView: View {
@EnvironmentObject var word : Word
var body: some View {
ZStack {
VStack {
VStack(alignment: .leading) {
ScrollView{
if word.guessedWords.count > 0 {
ForEach(0..<word.guessedWords.count, id: \.self) { count in
HStack {
Text(word.guessedWords[count])
.foregroundColor(.white)
.padding(.trailing, 10)
Text("\(count)")
.foregroundColor(.white)
}
}
}
}
.padding(.top, 75)
}
Spacer()
GuessedLetters()
Keyboard()
.padding(.bottom)
}
}
.background(.black)
.ignoresSafeArea()
}
}
#Preview {
ContentView()
.environmentObject(Word())
}

The alignment given to the
VStackdetermines how multiple views inside theVStackare aligned to each other, when they have different widths. However, if theVStackonly contains one subview (this being theScrollView), it has no effect.Here are three possible ways to fix:
1. Set a frame with
maxWidth: .infinityandalignment: .leadingon theScrollViewThis will push the contents of the
ScrollViewto the left, but the rows themselves will not be left aligned.2. Add a
Spacerto theHStackinside theScrollViewThis pushes the content to the left and the content will also be left aligned:
3. Use a nested
VStackinside theScrollViewThis is where
alignment: .leadingwill now be useful. You can also push it all to the left side usingmaxWidth: .infinity, as for variant 1: