VStack fill the width of the screen - SwiftUI
Generally when we use VStack
with few views we observed that there is some empty place left, here's how you can fix that without using tricky ways such as HStack
with a Spacer()
:
1. By using frame
struct ContentView: View {
var body: some View {
VStack {
Text("road2crypto.com")
}
.frame(
maxWidth: .infinity,
maxHeight: .infinity,
alignment: .topLeading
)
.background(Color.gray)
}
}
2. Use GeometryReader with frames
GeometryReader
returns a flexible preferred size to its parent layout
struct ContentView : View {
var body: some View {
GeometryReader { geometry in
VStack {
Text("road2crypto.com")
}
.frame(
width: geometry.size.width,
height: geometry.size.height,
alignment: .topLeading
)
}
.background(Color.gray)
}
}