sheldor
sheldor

Reputation: 159

Center VStack vertically?

import SwiftUI

struct Test: View {
    var body: some View {
        VStack{
            Text("dklf")
        }
        .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
    }
}

Why is the VStack not aligned in the middle of the Screen vertically?

enter image description here

Upvotes: 1

Views: 374

Answers (2)

Asperi
Asperi

Reputation: 258471

UIScreen.main.bounds does not account for the Safe Area, so this is what you probably wanted:

var body: some View {
    VStack{
        Text("dklf")
    }
    .frame(maxWidth: .infinity, maxHeight: .infinity)
}

Upvotes: 1

sheldor
sheldor

Reputation: 159

enter image description here

Ok thanks. I don't understand, why this blue border isn't at the top. If I add .statusBar(hidden: true) after the VStack, the blue border stays also there.

struct Test: View {
    var body: some View {
        VStack{
            Color.yellow
        }
        .ignoresSafeArea()
        .frame(maxWidth: .infinity, maxHeight: .infinity)
    }
}

Upvotes: 0

Related Questions