I am trying to add a footer that will extend to the very bottom of the screen, and tried some methods I found on Google and ChatGPT but still no luck. I added .edgesIgnoringSafeArea(.all)
but it doesn’t seem to do anything either.
VStack(spacing: 0) {
// Yellow rectangle footer
Rectangle()
.foregroundColor(.clear)
.frame(width: UIScreen.main.bounds.size.width, height: 16)
.background(Color(red:1, green: 0.78, blue: 0.17))
}.edgesIgnoringSafeArea(.all)
And also, is there a way to fix this footer in its position like using position: fixed
on HTML/CSS?
2
Answers
You can’t add something like a fixed position. In SwiftUI, everything was aligned. So in this case, simply add a
Space
aboveRectangle
like this:The reason why the yellow band is not extending into the safe area is because it is not touching it. You just need to add a
Spacer
to yourVStack
to push it down and it works.While we’re at it, you are using a couple of deprecated techniques in your example:
UIScreen.main.bounds.size
is deprecated, use aGeometryReader
instead.edgesIgnoringSafeArea
is deprecated, use.ignoresSafeArea
instead.In fact, you don’t need to know the screen size. If you want a view to occupy the maximum possible width then use
maxWidth: .infinity
instead. The same goes for height. You don’t even need.ignoresSafeArea
, because you are usingbackground(_:ignoresSafeAreaEdges:)
which ignores safe areas by default.Here is an updated version:
Using a
VStack
in this way is exactly how you can fix the footer to the bottom of the screen with SwiftUI. If the other content in theVStack
might need more height then you can put aScrollView
around it. AScrollView
is greedy and occupies as much space as possible (just like aSpacer
). So then the example would look like this: