So basically this is my code.
Text("Melbourne, Victoria")
.font(.largeTitle)
.fontWeight(.bold)
.foregroundColor(Color.white)
.padding(.bottom, 30)
Image(systemName: "moon.fill")
.foregroundColor(Color.white)
.font(.system(size: 60))
Text("Today")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
Text("34°C")
.font(.title3)
.fontWeight(.medium)
.foregroundColor(Color.white)
Spacer()
HStack {
Image(systemName: "sun.max.fill")
.foregroundColor(Color.yellow)
.font(.system(size: 40))
.padding(.bottom, 550)
Text("Mon 34°C")
.font(.title)
.fontWeight(.medium)
.foregroundColor(Color.white)
.padding(.bottom, 550)
}
}
.background(
Image("night")
.ignoresSafeArea()
)
I want to make more of the days in this weather app each with it’s on SF Symbol and everything is inside this VStack. And the days and SF symbols are inside a HStack to keep them horizontal. But if I want to put more of those the next time I do it they go next to each other but the symbol goes on top look at this.
This is the image with only 1 of those days
This is the one if I put them in another VStack which makes the SF symbol go above the text
IS there any solution to this?
2
Answers
The problem is your
.padding(.bottom, 550)
.It’s currently attached to your first
Image
. As a result, theHStack
is stretched vertically and it appears as though the firstImage
is higher up than second one.To fix, move both
.padding(.bottom, 550)
s to outside yourHStack
.You have quite a lot of arbitrary values for padding. You should have your layout adapt to various devices, rather than hard-coding that
.bottom
spacing for example.Working example:
Result:
Although there is already an outer
VStack
, they are nestedVStack
s because they have different amounts of padding. Multiple views are grouped together, and then those are grouped together with larger paddings to separate them.Bonus
SF Symbols colors
Use:
Rather than:
To color the SF Symbols with the default colors, as shown in the above example.
Align days
You can align the days across each
HStack
with a custom alignment guide. Use the following:Making the other following changes:
Result: