2

I already searched, but can't find a solution that works.

I have this super simple code. When I add the NavigationView, the background color goes away.

I think if I can make the background color of NavigationView Transparent, it would solve the issue.

struct TestView: View {
    
    var body: some View {
        
        VStack {
            Spacer()
            NavigationView {
                Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
            }

                
            
            
            Spacer()
        }
        .background(Color(red: 128 / 255, green: 27 / 255, blue: 44 / 255))
        .ignoresSafeArea(.all)
        
    }
}

Thanks in advance

  • Normally, you would have `NavigationView` be the outermost parent view. Is there a reason that you're trying to make it a child view? – jnpdx Feb 08 '22 at 20:00
  • Hi again @jnpdx. I just added the VStack because it's the only way I know to set a background color. Is there a way to add a background color to NavigationView? – Diego Alcubierre Feb 08 '22 at 20:12
  • 1
    This looks like a duplicate of: https://stackoverflow.com/questions/56923397/how-change-background-color-if-using-navigationview-in-swiftui Note that there may not be a perfect solution to this, but there's a ton of discussion there about different options. – jnpdx Feb 08 '22 at 20:22
  • @jnpdx thank you!!! – Diego Alcubierre Feb 08 '22 at 22:39

1 Answers1

1

I had the exact same issue. I managed to solve this by keeping the 'NavigationView' as the parent and having the ZStack as the child for your background

So to take your code:

    
    var body: some View {
        
        Spacer()
        NavigationView {
            
            ZStack {
                //Your colour here + Safezone ignore
            }
            VStack {
                Text("Hello, World!")
                Spacer()
            }
            
            
        }
    }
}