I want the status bar and at the bottom to be white (Same as root background color), but no idea, do i need to get status bar height and add margin top and bottom?
Here is my code and the preview below
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(
alignment: .leading,
spacing: 10
) {
Text("Title")
.font(
.system(size: 32)
.weight(.heavy)
)
Text("Content")
}
.frame(
minWidth: 0,
maxWidth: .infinity,
minHeight: 0,
maxHeight: .infinity,
alignment: .topLeading
)
.padding(
EdgeInsets(
top: 0,
leading: 20,
bottom: 0,
trailing: 20
)
)
.background(Color.gray)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
}
}
}
3
Answers
If it were me tackling this kind of UI, I would use some other nice Views that SwiftUI provides for us (like
ZStack
).The
ZStack
places objects one on top of another from the bottom up. So you would want your color first, then theVStack
after. It would look something like this:add a vertical padding of 1:
Let me add another answer that goes with a different approach, without using
.frame()
. Instead, it uses the full width and height ofHStack
andVStack
to fill the screen. For the status bar and the bottom area, this approach uses a.layoutPriority()
modifier to the gray color but not allowing it to overlap the safe area.While the other answers work quite fine, my purpose with this example is to open the range of possibilities.