how to move this stack top of the screen in swiftUI. below my code there. [1]: https://i.stack.imgur.com/wBD2c.png
Asked
Active
Viewed 3,208 times
-1
-
2Post the code in your question, not pictures of code. Actual code can be cut and pasted and tried easily without retyping. Also, actual code can be searched, but pictures can't. – vacawama Oct 16 '20 at 02:58
3 Answers
1
You can remove the HStack
and place the Spacer() within your VStack
which will force the view to the top.
var body: some View {
VStack {
Text("Hello World")
Text("Test")
Spacer()
}
}
Or if you want to keep the View to the left of the screen you can use another VStack and Spacer()
var body: some View {
VStack {
HStack {
VStack {
Text("Hello World")
Text("Test")
Spacer()
}
Spacer()
}
}
}
If you are just getting started with SwiftUI I highly recommend Paul Hudson's 100 Days of SwiftUI. It's free and will teach you a lot about SwiftUI.

pawello2222
- 46,897
- 22
- 145
- 209

Dan O'Leary
- 916
- 9
- 20
1
You just need to add Spacer()
& embed your view in VStack & HStack to force push the view to the top.
VStack {
VStack {
//Your VStack
}
Spacer()
}

Navemics
- 51
- 6
0
You can apply max frame with alignment for the container or to the child view directly.
VStack {
Text("Hello world")
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
Text("Hello, World!").frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)

Valerika
- 366
- 1
- 3
- 8