0

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

Another Dude
  • 1,204
  • 4
  • 15
  • 33
  • https://stackoverflow.com/questions/56437036/swiftui-how-do-i-change-the-background-color-of-a-view – KevinP Oct 22 '20 at 09:45
  • Does this answer your question? [SwiftUI - How do I change the background color of a View?](https://stackoverflow.com/questions/56437036/swiftui-how-do-i-change-the-background-color-of-a-view) – Glenn Posadas Oct 22 '20 at 09:46

2 Answers2

2

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.

Another Dude
  • 1,204
  • 4
  • 15
  • 33
0

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

the.blaggy
  • 815
  • 5
  • 16