-1

I am trying to do something very simple, display a view with a background color, and in the center of this view, display a single label.

I tried this:

var body: some View {
    VStack {
        Text("Hello!")
    }
    .background(MyColors.blue)
    .ignoresSafeArea() 
}

Here is the given result:

enter image description here

What am I doing wrong?

Thank you for your help

2

2 Answers 2

3

I managed to get it working with this code:

var body: some View {
    ZStack {
        Style.Color.red
        VStack {
            Text("Hello!")
        }
    }
    .ignoresSafeArea()
}

I didn't understand first that background and rest of the view must be set on a different z-axis.

Sign up to request clarification or add additional context in comments.

Comments

1

You can do it like this:

Text("Hello!")
    .frame(maxWidth: .infinity, maxHeight: .infinity)
    .background(MyColors.blue)
    .ignoresSafeArea()

Source: https://www.hackingwithswift.com/articles/224/common-swiftui-mistakes-and-how-to-fix-them

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.