Navigation View background Color

旧巷老猫 提交于 2020-08-09 09:05:21

问题


I'm try to set a background color on my SwiftUi List, as for my post here: SwiftUI Background List color

I found a solution inserting the following code as init()

   init() {
        UITableView.appearance().backgroundColor = UIColor.clear
        UITableViewCell.appearance().backgroundColor = .clear
    }

my issue now is ... as soon I insert a Navigation Link the background color again became white.

how to set the color to .clear to the NavigationView? I have tried to .foregroundColor(.clear) but noting...

what I want is having a nav link working with no white background.. like this

but actually it does like this :

struct ContentView: View {

    var dm : DataManager

    init(dmi: DataManager) {
        self.dm = dmi
        UITableView.appearance().backgroundColor = UIColor.clear
        UITableViewCell.appearance().backgroundColor = .clear

    }
    var body: some View {

        ZStack{
            RadialGradient(gradient: Gradient(colors: [.orange, .red]), center: .center, startRadius: 100, endRadius: 470).edgesIgnoringSafeArea(.all)
                .overlay(
                    // NavigationView{
                    List{
                        ForEach(dm.vector, id: \.self) {  item in
                            Text(String(item))
                        }

                    }
                    //                    }
            )
        }

    }
}


回答1:


It is easy. I don't suggest you to do it, you better follow apple ui design recommendation

struct ContentView: View {

    init() {
        UITableView.appearance().backgroundColor = UIColor.clear
        UITableViewCell.appearance().backgroundColor = .clear

    }
    var body: some View {
        NavigationView {
            RadialGradient(gradient: Gradient(colors: [.orange, .red]), center: .center, startRadius: 100, endRadius: 470).edgesIgnoringSafeArea(.all)
                .overlay(
                    List{
                        Text("Alfa")
                        NavigationLink(destination: Text("LINKED")) {
                            Text("Label")
                        }

                    }.navigationBarTitle("Table")
            )
        }
    }
}




回答2:


Looking in the view debugger it appears that the hierarchy of views that NavigationView creates includes a view with a white background that covers the entire screen and cannot be modified.

However, I was able to get around this by setting the background for the entire screen on the individual views within the navigation stack, which overlays the overlay created by the navigation view haha.

Here's an example using a red background:

struct ContentView: View {
  var body: some View {
    NavigationView {
      ZStack {
        Color.red.edgesIgnoringSafeArea(.all)
        ListView {          
          Text("Hello, world!")
        }
      }
    }
  }
}


来源:https://stackoverflow.com/questions/60150487/navigation-view-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!