11

I'm trying to change background color of the NavigationView (not navigation bar) using this code:

NavigationView {
    Text("Text")
}
.background(Color.clear)

But it doesn't work. Also, I tried to change UIView appearance:

UIView.appearance().backgroundColor = UIColor.black

But it doesn't work too.

The actual result is presented below:

enter image description here

The desired result is:

enter image description here

Does anyone know how to do this?

Ilya Kharabet
  • 4,203
  • 3
  • 15
  • 29
  • Possible duplicate of [SwiftUI: How do you change the tint color (background color) of a NavigationView?](https://stackoverflow.com/questions/56503479/swiftui-how-do-you-change-the-tint-color-background-color-of-a-navigationview) – Rocky Aug 28 '19 at 06:02
  • @IIya Please check this [https://stackoverflow.com/questions/61812677/change-background-color-of-view-inside-tabview-having-navigationview-and-scrollv?noredirect=1#comment109334412_61812677] – Sona May 15 '20 at 11:38
  • @IIya - Did you find a solution ?? Even i have a view which is behind NavigationView and I want to make it visible my setting Navigation View background as clear color . – Maddiee Jul 03 '20 at 10:01

1 Answers1

7

First look at this result:

View hierarchy

As you can see, you can set the color of each element in the View hierarchy like this:

struct ContentView: View {
    
    init(){
        UINavigationBar.appearance().backgroundColor = .green 
        //For other NavigationBar changes, look here:(https://stackoverflow.com/a/57509555/5623035)
    }

    var body: some View {
        ZStack {
            Color.yellow
            NavigationView {
                ZStack {
                    Color.blue
                    Text("Some text")
                }
            }.background(Color.red)
        }
        // iOS 16 - No need for tweaking the appearance
        /* .toolbarBackground(.green, in: .navigationBar) */
    }
}

And the first one is window:

window.backgroundColor = .magenta

The issue you faced is we can not remove the background color of SwiftUI's HostingViewController (yet), so you can't see the navigationView (What you called) through the views hierarchy. You should wait for the API or try to fake the navigation view (not recommended).

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278